简体   繁体   中英

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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM