Table of Contents

Syntax

string htmlspecialchars(string $text [, int $quoting_style [, string $characters [, bool $doubleencode] ] ])

(PHP 4 , PHP 5)

$text Source string to encode special HTML characters.
$quoting_style Controls the encoding of quote characters (see below).
$characters The character set to use when encoding the string (see below).
$doubleencode Turn this off (pass false) to prevent the encoding from re-encoding text that has already been encoded.
RETURNS The source string with HTML encodings.

Possible values for $quoting_style

ENT_COMPAT Convert only double quotes and don't convert single-quotes.
ENT_QUOTES Convert all types of quotes (both double and single).
ENT_NOQUOTES Don't convert any quote characters in the source string.

Possible Values for $characters

ISO-8859-1 Western European, Latin-1
ISO-8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1(ISO-8859-1).
UTF-8 ASCII compatible multi-byte 8-bit Unicode.

cp866 | DOS-specific Cyrillic charset. This charset is supported in 4.3.2. |

cp1251 Windows-specific Cyrillic charset. This charset is supported in 4.3.2.
cp1252 Windows specific charset for Western European.
KOI8-R Russian. This charset is supported in 4.3.2.
BIG5 Traditional Chinese, mainly used in Taiwan.
GB2312 Simplified Chinese, national standard character set.
BIG5-HKSCS Big5 with Hong Kong extensions, Traditional Chinese.
Shift_JIS Japanese
EUC-JP Japanese

What it Does

This function encodes standard text as HTML encoded text. Text needs to be HTML encoded when it contains special characters that are used for HTML like angle brackets (< and >) that you don't want to be interpreted as HTML by the web browser.

This function only does basic HTML characters which are angle brackets (< and >), quotes ( and '), and the ampersand (&). If you require a comprehensive encoding, you should use htmlentities instead.

Example

// HTML encode text (for display in browser)
echo('Encoded text: ' . htmlspecialchars($str));
 
htmlspecialchars.txt · Last modified: Apr 18, 2008 - 2:00pm (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki