Thursday, June 27, 2013

htmlentities & htmlspecialchars in php

htmlentities 

Convert all applicable characters to HTML entities

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

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



htmlspecialchars 

Convert special characters to HTML entities

'&' (ampersand) becomes '&amp;' 
'<' (less than) becomes '&lt;' 

Example
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; //Outputs:  &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;

No comments: