Uday Hiwarale
Uday Hiwarale

Reputation: 233

htmlspecialchars in php not decoding

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,

Upvotes: 1

Views: 160

Answers (1)

jeroen
jeroen

Reputation: 91734

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;.

Upvotes: 1

Related Questions