小白教程
所有教程
关于
Search
172.70.130.249
172.70.130.249
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Func string htmlentities”的源代码
本页内容
上一节:
Func_string_html_entity_decode
下一节:
Func_string_htmlspecialchars_decode
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:htmlentities()}}[[分类:PHP字符串函数参考手册|21]] = PHP htmlentities() 函数 = [[:分类:PHP字符串函数参考手册]] 把一些字符转换为 HTML 实体: <sample title="" desc="" lang="php" hererun="1"> <?php $str = "<© W3CSçh°°¦§>"; echo htmlentities($str); ?> </sample> 上面代码的 HTML 输出如下(查看源代码): <sample title="" desc="" lang="php" hererun="1"> <!DOCTYPE html> <html> <body> <© W3CSçh°°¦§> </body> </html> </sample> 上面代码的浏览器输出如下: <sample title="" desc="" lang="php" hererun="1"> <© W3CSçh°°¦§> </sample> == 定义和用法 == htmlentities() 函数把字符转换为 HTML 实体。 '''提示:'''要把 HTML 实体转换回字符,请使用 [[func string html entity decode|html_entity_decode()]] 函数。 '''提示:'''请使用 [[func string get html translation table|get_html_translation_table()]] 函数来返回 htmlentities() 使用的翻译表。 == 语法 == <pre> htmlentities(string,flags,character-set,double_encode) </pre> {| class="table table-striped table-hover" !width="50%"| 参数 !width="50%"| 描述 |- | string | 必需。规定要转换的字符串。 |- | flags | 可选。规定如何处理引号、无效的编码以及使用哪种文档类型。 可用的引号类型: * ENT_COMPAT - 默认。仅编码双引号。 * ENT_QUOTES - 编码双引号和单引号。 * ENT_NOQUOTES - 不编码任何引号。 无效的编码: * ENT_IGNORE - 忽略无效的编码,而不是让函数返回一个空的字符串。应尽量避免,因为这可能对安全性有影响。 * ENT_SUBSTITUTE - 把无效的编码替代成一个指定的带有 Unicode 替代字符 U+FFFD(UTF-8)或者 &#FFFD; 的字符,而不是返回一个空的字符串。 * ENT_DISALLOWED - 把指定文档类型中的无效代码点替代成 Unicode 替代字符 U+FFFD(UTF-8)或者 &#FFFD;。 规定使用的文档类型的附加 flags: * ENT_HTML401 - 默认。作为 HTML 4.01 处理代码。 * ENT_HTML5 - 作为 HTML 5 处理代码。 * ENT_XML1 - 作为 XML 1 处理代码。 * ENT_XHTML - 作为 XHTML 处理代码。 |- | character-set | 可选。一个规定了要使用的字符集的字符串。 允许的值: * UTF-8 - 默认。ASCII 兼容多字节的 8 位 Unicode * ISO-8859-1 - 西欧 * ISO-8859-15 - 西欧(加入欧元符号 + ISO-8859-1 中丢失的法语和芬兰语字母) * cp866 - DOS 专用 Cyrillic 字符集 * cp1251 - Windows 专用 Cyrillic 字符集 * cp1252 - Windows 专用西欧字符集 * KOI8-R - 俄语 * BIG5 - 繁体中文,主要在台湾使用 * GB2312 - 简体中文,国家标准字符集 * BIG5-HKSCS - 带香港扩展的 Big5 * Shift_JIS - 日语 * EUC-JP - 日语 * MacRoman - Mac 操作系统使用的字符集 '''注释:'''在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。 |- | double_encode | 可选。一个规定了是否编码已存在的 HTML 实体的布尔值。 * TRUE - 默认。将对每个实体进行转换。 * FALSE - 不会对已存在的 HTML 实体进行编码。 |} == 技术细节 == {| class="table table-striped table-hover" | 返回值: | 返回已转换的字符串。如果 string 包含无效的编码,则返回一个空的字符串,除非设置了 ENT_IGNORE 或者 ENT_SUBSTITUTE 标志。 |- | PHP 版本: | 4+ |- | 更新日志: | 在 PHP 5 中,character-set 参数的默认值改为 UTF-8。在 PHP 5.4 中,新增了:ENT_SUBSTITUTE、ENT_DISALLOWED、ENT_HTML401、ENT_HTML5、ENT_XML1 和 ENT_XHTML。在 PHP 5.3 中,新增了 ENT_IGNORE。在 PHP 5.2.3 中,新增了 double_encode 参数。在 PHP 4.1 中,新增了 character-set 参数。 |} == 更多实例 == == 实例 1 == 把一些字符转换为 HTML 实体: <sample title="" desc="" lang="php" hererun="1"> <?php $str = "Jane & 'Tarzan'"; echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes echo "<br>"; echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes echo "<br>"; echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes ?> </sample> 上面代码的 HTML 输出如下(查看源代码): <sample title="" desc="" lang="php" hererun="1"> <!DOCTYPE html> <html> <body> Jane & 'Tarzan'<br> Jane & 'Tarzan'<br> Jane & 'Tarzan' </body> </html> </sample> 上面代码的浏览器输出如下: <sample title="" desc="" lang="php" hererun="1"> Jane & 'Tarzan' Jane & 'Tarzan' Jane & 'Tarzan' </sample> == 实例 2 == 通过使用西欧字符集,把一些字符转换为 HTML 实体: <sample title="" desc="" lang="php" hererun="1"> <?php $str = "My name is Øyvind Åsane. I'm Norwegian."; echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); // Will only convert double quotes (not single quotes), and uses the character-set Western European ?> </sample> 上面代码的 HTML 输出如下(查看源代码): <sample title="" desc="" lang="php" hererun="1"> <!DOCTYPE html> <html> <body> My name is Øyvind Åsane. I'm Norwegian. </body> </html> </sample> 上面代码的浏览器输出如下: <sample title="" desc="" lang="php" hererun="1"> My name is Øyvind Åsane. I'm Norwegian. </sample> [[:分类:PHP字符串函数参考手册]]
返回至“
Func string htmlentities
”。
上一节:
Func_string_html_entity_decode
下一节:
Func_string_htmlspecialchars_decode