Htmlspecialchars (), Htmlspecialchars_decode ()
Definition and usage
The Htmlspecialchars () function converts some pre-defined characters to HTML entities.
The predefined characters are:
& (and number) becomes &
"(double quotes) becomes"
' (single quote) become '
< (less than) becomes <
> (greater than) becomes >
Grammar
Htmlspecialchars (String,quotestyle,character-set)
Parameter description
string is required. Specifies the string to convert.
Quotestyle is optional. Specifies how to encode single and double quotation marks.
Ent_compat-Default. Encodes only double quotes.
Ent_quotes-encodes double and single quotation marks.
Ent_noquotes-do not encode any quotation marks.
Character-set is optional. A string value that specifies the character set to use.
Iso-8859-1-Default. Western europe.
Iso-8859-15-Western Europe (added Euro symbol and French, Finnish alphabet).
UTF-8-ASCII-compatible multi-byte 8-bit Unicode
Cp866-dos dedicated Cyrillic Character Set
Cp1251-windows dedicated Cyrillic Character Set
Cp1252-windows Special Western European character set
Koi8-r-Russian
GB2312-Simplified Chinese, national standard character set
BIG5-Traditional Chinese
Big5-hkscs-big5 Hong Kong expansion
Shift_JIS-Japanese
EUC-JP-Japanese
Hints and Notes
Hint: A character set that cannot be recognized is ignored and replaced by Iso-8859-1.
Example
PHP code
$str = "John & ' Adams '";
Echo Htmlspecialchars ($str, Ent_compat);
echo "
";
Echo Htmlspecialchars ($str, ent_quotes);
echo "
";
Echo Htmlspecialchars ($str, ent_noquotes);
?>
Browser output:
John & ' Adams '
John & ' Adams '
John & ' Adams '
If you view the source code in a browser, you will see these HTML:
PHP code
John & ' Adams '
John & ' Adams '
John & ' Adams '
Definition and usage
The Htmlspecialchars_decode () function converts some pre-defined HTML entities to characters.
The HTML entities that will be decoded are:
& Become & (and No.)
"Become" (double quotes)
' Become ' (single quote)
< becomes < (less than)
> Become > (Greater than)
Grammar
Htmlspecialchars_decode (String,quotestyle)
Parameter description
string is required. Specifies the string to decode.
Quotestyle is optional. Specifies how to decode single and double quotation marks.
Ent_compat-Default. Decodes only double quotes.
Ent_quotes-decodes double and single quotes.
Ent_noquotes-Do not decode any quotation marks.
Example
PHP code
$str = "John & ' Adams '";
echo Htmlspecialchars_decode ($STR);
echo "
";
Echo Htmlspecialchars_decode ($str, ent_quotes);
echo "
";
Echo Htmlspecialchars_decode ($str, ent_noquotes);
?>
Browser output:
John & ' Adams '
John & ' Adams '
John & ' Adams '
If you view the source code in a browser, you will see these HTML:
PHP code
John & ' Adams '
John & ' Adams '
John & ' Adams '
http://www.bkjia.com/PHPjc/478675.html www.bkjia.com true http://www.bkjia.com/PHPjc/478675.html techarticle The htmlspecialchars (), Htmlspecialchars_decode () Definition and usage htmlspecialchars () function converts some of the predefined characters to HTML entities. The predefined characters are: (and number) into ...