PHP » Strings » htmlspecialchars()

Syntax:
string htmlspecialchars(string s [, int quote_style [, string characterset]])
s
String with html special characters.
quote_style
How to encode quotes.
characterset
Character set to use.

Escapes html special characters.

Sometimes it is necessary to show < > brackets and other characters that have a special meaning in html. It can also be necessary to show the html code itself. To aid in this, htmlspecialchars() escapes these special characters in a string so that they are not interpreted by the web browser. The quote_style parameter specifies whether quotes should be escaped. The possible values for quote_style are ENT_COMPAT, which only escapes double quotes, ENT_NOQUOTES, for no quote escaping, and ENT_QUOTES for escaping both double and single quotes. The characterset parameter specifies a character set to use for the translation. Default is ISO-8859-1.

Examples

Code:
<?php

$s = "hello<br>world";

print "$s<br><br>";
print htmlspecialchars($s);

?>
Output:
hello
world

hello<br>world
Explanation:

The second print statement shows the effect that htmlspecialchars() has.

See Also: