PHP htmlentities not working even with parameters

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;


I thought I had the same problem as Pjack (msg of Jul 14 at 8:54):

$str = "A 'quote' is <b>bold</b>";
echo htmlentities($str); 

gives in the Browser (Firefox in my case) the original string $str (without any translation), while

echo htmlentities(htmlentities($str));

gives:

A 'quote' is &lt;b&gt;bold&lt;/b&gt; 

(I use PHP/5.4.16 obtained from windows-7 XAMPP).

However, after some more thought it occurred to me that the Browser shows the strings &lt; and &gt; as > and <. (See the source code in the browser). Second call of htmlentities translates & into &amp; and only then the Browser shows what you expected in the first place.


Your code works for me :-?

In the manual page for htmlentities() we can read:

Return Values

Returns the encoded string.

If the input string contains an invalid code unit sequence within the given encoding an empty string will be returned, unless either the ENT_IGNORE or ENT_SUBSTITUTE flags are set.

My guess is that the input data is not properly encoded as UTF-8 and the function is returning an empty string. (Assuming that the script is not crashing, i.e., code after that part still runs.)