5

How will I convert all special characters to their corresponding html entity?

Special character would be like $ & / \ { } ( - ' , @ etc.

I tried to use htmlentities() and htmlspecialchars(). But didn't solve my problem.

please check here. I want output like Entity Number i.e. column 3.

Actually the scenario is - I need to take input from fckeditor. and then save into the database. So I need to convert all special character to their corresponding html entity, from the text. Otherwise it's giving me error.

Ripa Saha
  • 2,532
  • 6
  • 27
  • 51
  • 2
    `htmlspecialchars()` ? http://in3.php.net/manual/en/function.htmlspecialchars.php – Shankar Narayana Damodaran Jan 24 '14 at 10:12
  • @Shankar Damodaran , I forgot to mention - I also tried htmlspecialchars(). but not get output – Ripa Saha Jan 24 '14 at 10:15
  • What is the expected output of `$` you are looking for as a HTML entity ? – Shankar Narayana Damodaran Jan 24 '14 at 10:16
  • for $ it would give $ – Ripa Saha Jan 24 '14 at 10:19
  • Get the character's code point, then format an SGML entity yourself by prepending `` and appending semicolon. For a head start, adjust the function proposed here: http://stackoverflow.com/questions/395832/how-to-get-code-point-number-for-a-given-character-in-a-utf-8-string – Ruud Helderman Jan 24 '14 at 10:29
  • @Ruud that's not suitable for me now. Because I need to convert special characters from an fckeditor input. – Ripa Saha Jan 24 '14 at 10:37
  • What about: `echo htmlspecialchars("" . ord('$') . ";");` – divaka Jan 24 '14 at 10:43
  • @divaka before commenting please check question properly. – Ripa Saha Jan 24 '14 at 10:44
  • @ripa - My comment is an example? What's wrong with it? Check the code for every special char you want. – divaka Jan 24 '14 at 10:47
  • @ripa Test it here for every symbol you want and see the output: http://writecodeonline.com/php/ then compare with the third column here http://www.freeformatter.com/html-entities.html and tell me what I'm misunderstanding? – divaka Jan 24 '14 at 10:49
  • @divaka in my question, I've already mentioned htmlspecialchars() not working for me. – Ripa Saha Jan 24 '14 at 10:51
  • @ripa did you even test the code?? Pretty sure the way I wrote it is different from the way you've tested. – divaka Jan 24 '14 at 10:51
  • @ripa: I have a strong feeling you are not supplying us with all the details necessary to answer your question. In one of your comments you mentioned fckeditor; you did not tag it as such, nor was it mentioned in the question itself. Please edit your question; currently people are speculating about your intentions, which is a total waste of everybody's valuable time IMHO. – Ruud Helderman Jan 24 '14 at 14:26
  • @ripa: Thanks for your edit, that really helps; though it still leaves me with many questions. You say you got an error while trying to store text in a database. What error? Which database engine? Accessed through which API? What makes you think HTML-encoding is the solution? Let's take a few steps back, and focus on the error before jumping to conclusions. Also, please show us some code where you write to the database; code often speaks louder than words. – Ruud Helderman Jan 24 '14 at 19:09
  • I agree with @Ruud. What exactly is the problem you're encountering? It sounds like you have content being edited with FCKEditor -- give us an example of a problem with the text it returns. There are only a few things which need to be handled before shoving it into a database, so why are you trying to replace so many characters? – Phil Perry Jan 24 '14 at 19:43
  • @ripa: No you didn't. At least not since my previous comment. Forgot to save your changes? – Ruud Helderman Jan 25 '14 at 08:47

1 Answers1

1

What you are looking is for an ASCII equivalent of a character. So you need to make use of ord().

By the way what divaka mentioned is right.

Do like this..

<?php

function getHTMLASCIIEquiv($val)
{
    $arr=['$','&','/','\\','{','}','(','-','\'',',','@'];
    $val = str_split($val);$str="";
    foreach($val as $v)
    {
        if(in_array($v,$arr))
        {
        $str.="&#".ord($v).";";
        }
        else
        {
            $str.=$v;
        }
    }
    return $str;
}

echo getHTMLASCIIEquiv('please check $100 & get email from test@cc.com');

OUTPUT :

please check &#36;100 &#38; get email from test&#64;cc.com

Demo

Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126