1

I have a application where I store sting as it is but while dispying, I want special characters to be converted to their HTML name like for < will be &lt. To achieve it, I am using php inbuilt function htmlspecialchars.

Output of text with this function is achieved with following code

$reviewTxt = htmlspecialchars($reviewTxt);
echo $reviewTxt; 

Now, for reviewTxt to be 'I loved you <3', it should produce I loved you &lt;3 but should display the original text. In my case, it displays the encoded data I loved you &lt;3. I also tried to paste I loved you &lt;3 instead of above php code just to see if I can get original text and yes, it shows 'I loved you <3'.

I am not sure what I am missing,

1
  • 1
    The conversion from &lt; to >should be done by your web browser. Are you using your web browser to view the output of your php script? Commented Nov 19, 2014 at 20:53

1 Answer 1

1

It looks like you are encoding twice with htmlspecialchars() / htmlentities().

That causes the & symbol of the first result to be encoded in the second result, giving you a string like I loved you &amp;lt;3.

So it will show the encoded & followed by the litteral string lt;.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.