...

View Full Version : htmlspecialchars()



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

dragon
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?

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

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

brothercake
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:


echo('<xmp>');
var_dump($new);
echo('</xmp>');

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

http://us2.php.net/manual/en/function.htmlentities.php


<?php
$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);
?>

chump2877
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum