定义和用法
htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。
预定义的字符是:
•& (和号) 成为 &
•" (双引号) 成为 "
•' (单引号) 成为 '
••> (大于) 成为 >
语法
htmlspecialchars(string,quotestyle,character-set)
参数 |
描述 |
string |
必需。规定要转换的字符串。 |
quotestyle |
可选。规定如何编码单引号和双引号。
- ENT_COMPAT - 默认。仅编码双引号。
- ENT_QUOTES - 编码双引号和单引号。
- ENT_NOQUOTES - 不编码任何引号。
|
character-set |
可选。字符串值,规定要使用的字符集。
- ISO-8859-1 - 默认。西欧。
- ISO-8859-15 - 西欧(增加 Euro 符号以及法语、芬兰语字母)。
- UTF-8 - ASCII 兼容多字节 8 比特 Unicode
- cp866 - DOS 专用 Cyrillic 字符集
- cp1251 - Windows 专用 Cyrillic 字符集
- cp1252 - Windows 专用西欧字符集
- KOI8-R - 俄语
- GB2312 - 简体中文,国家标准字符集
- BIG5 - 繁体中文
- BIG5-HKSCS - Big5 香港扩展
- Shift_JIS - 日语
- EUC-JP - 日语
|
提示和注释
提示:无法被识别的字符集将被忽略,并由 ISO-8859-1 代替。
例子
$str = "John & 'Adams'";
echo htmlspecialchars($str, ENT_COMPAT);
echo "
";
echo htmlspecialchars($str, ENT_QUOTES);
echo "
";
echo htmlspecialchars($str, ENT_NOQUOTES);
?>
浏览器输出:
John & 'Adams'
John & 'Adams'
John & 'Adams'
如果在浏览器中查看源代码,会看到这些 HTML:
John & 'Adams'
John & 'Adams'
John & 'Adams'
=======================================================================
shtmlspecialchars() 函数正好相反
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn