PHP htmlentities not working even with parameters

ToddN picture ToddN · Apr 4, 2013 · Viewed 16.5k times · Source

Of course this has been asked before and have searched for solutions, all which have not worked thus far. I want to change out the TM symbol and the ampersand to their html equivelents by using htmlentities or htmlspecialchars:

$TEST = "Kold Locker™ & other stuff";
echo "ORGINIAL: " . $TEST . "<BR/>";

echo "HTML: " . htmlentities($TEST, ENT_COMPAT, 'UTF-8');

This displays:

ORGINIAL: Kold Locker™ & other stuff
HTML: 

I have also tried it with htmlspecialchars and the second parameter changed with the same result.

What am I missing that others have claimed worked in other solutions?

UPDATE: I tried just displaying utf8_encode($TEST) and it displayed HTML: Kold Locker™ & other stuff

Answer

internals-in picture internals-in · Jun 4, 2013

I dont know why , this worked for me (htmlentities has to be called twice for me)

$html="<html> <head><head>something like this   </html>"
$entities_correction= htmlentities( $html, ENT_COMPAT, 'UTF-8');
echo  htmlentities( $entities_correction, ENT_COMPAT, 'UTF-8');

output :

&lt;html&gt; &lt;head&gt;&lt;head&gt;something like this &lt;/html&gt;