View Full Version : htmlspecialchars()

02-19-2006, 10:00 AM
I'm trying to execute this code...it's straight out of the PHP manual:

$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new;

This outputs:

<a href='test'>Test</a>

I'm running PHP 5....is there some reason that htmlspecialchars() is not working here....a setting in the php.ini perhaps? I'm ready to pull my hair out over this, it doesn;t make sense...

02-19-2006, 11:20 AM
The echo is sending the output to the browser, so the browser is interpreting it correctly. Is this the output that the browser is displaying or the source code of the html?

02-19-2006, 11:57 AM
huh? I was under the impression that the output should be as it is on the following page (look for the example, it's identical): http://us2.php.net/manual/en/function.htmlspecialchars.php

what I've shown here is what the browser is outputing...there is no "HTML source code" involved here, this is purely PHP...my file consists of these 2 lines and nothing else...

02-19-2006, 01:01 PM
Are you viewing the output through a browser though? The browser will interpret the ASCII into what you're seeing as output. Right-click on the browser and view source. Alternatively, try sending the output directly to a text file and then read it from there.

02-19-2006, 01:30 PM
Or you could try the <xmp> tag - it's not valid or standard, but some browsers implement it and it's useful for testing - it outputs literal code, exactly as it appears in source:


02-19-2006, 03:32 PM
You could try...


$str = "A 'quote' is <b>bold</b>";

// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str);

// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;
echo htmlentities($str, ENT_QUOTES);

02-20-2006, 11:32 AM
Sorry, guys, you were right....the browser interpreted the ascii and rendered the HTML...if I view source, everything has been encoded correctly...:o