PHP htmlspecialchars() 函数实例代码及用法大全

实例

把预定义的字符 "<" (小于)和 ">" (大于)转换为 HTML 实体:

rush:PHP;"> PHP $str = "This is some bold text."; echo htmlspecialchars($str); ?>

以上代码的 HTML 输出如下(查看源代码):

rush:PHP;"> This is some bold text.

以上代码的浏览器输出

This is some bold text.

运行实例

定义和用法

htmlspecialchars() 函数把预定义的字符转换为 HTML 实体。

预定义的字符是:

& (和号)成为 & " (双引号)成为 " ' (单引号)成为 ' < (小于)成为 < > (大于)成为 >

提示:如需把特殊的 HTML 实体转换回字符,请使用 htmlspecialchars_decode() 函数

语法

htmlspecialchars(string,flags,character-set,double_encode)

normal; word-spacing: 0px; border-collapse: collapse; border-bottom: rgb(170,170) 1px solid; text-transform: none; font-weight: normal; color: rgb(0,0); padding-bottom: 0px; font-style: normal; text-align: left; padding-top: 0px; padding-left: 0px; margin: 15px 0px 0px; border-left: rgb(170,170) 1px solid; orphans: 2; widows: 2; letter-spacing: normal; padding-right: 0px; background-color: rgb(253,252,248); text-indent: 0px; font-variant-ligatures: normal; font-variant-caps: normal; -webkit-text-stroke-width: 0px"> ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px"> ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,245,245)"> ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,255)">
ottom: rgb(63,63) 1px solid; color: rgb(255,255,255); padding-bottom: 5px; text-align: left; padding-top: 5px; padding-left: 6px; margin: 0px; border-left: rgb(63,63) 1px solid; padding-right: 15px; background-color: rgb(63,63)">参数 ottom: rgb(63,63)">描述 ottom: rgb(170,170) 1px solid; padding-bottom: 6px; padding-top: 6px; padding-left: 6px; margin: 0px; border-left: rgb(170,170) 1px solid; padding-right: 15px">ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">stringottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,245)"> ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">flagsottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; line-height: 21px; padding-right: 0px">可选。规定如何处理引号、无效的编码以及使用哪种文档类型。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">可用的引号类型:

    ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 20px; border-left: 0px; padding-right: 0px">
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_COMPAT - 认。仅编码双引号。
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_QUOTES - 编码双引号和单引号。
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_NOQUOTES - 不编码任何引号。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">无效的编码:

    ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 20px; border-left: 0px; padding-right: 0px">
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_IGnorE - 忽略无效的编码,而不是让函数返回一个空的字符串。应尽量避免,因为这可能对安全性有影响。
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_SUBSTITUTE - 把无效的编码替代成一个指定的带有 Unicode 替代字符 U+FFFD(UTF-8)或者 &#FFFD; 的字符,而不是返回一个空的字符串。
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_disALLOWED - 把指定文档类型中的无效代码点替代成 Unicode 替代字符 U+FFFD(UTF-8)或者 &#FFFD;。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">规定使用的文档类型的附加 flags:

    ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 20px; border-left: 0px; padding-right: 0px">
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_HTML401 - 认。作为 HTML 4.01 处理代码
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_HTML5 - 作为 HTML 5 处理代码
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_XML1 - 作为 XML 1 处理代码
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ENT_XHTML - 作为 XHTML 处理代码
ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">character-setottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; line-height: 21px; padding-right: 0px">可选。一个规定了要使用的字符集的字符串。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">允许的值:

    ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 20px; border-left: 0px; padding-right: 0px">
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">UTF-8 - 认。ASCII 兼容多字节的 8 位 Unicode
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ISO-8859-1 - 西欧
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">ISO-8859-15 - 西欧(加入欧元符号 + ISO-8859-1 中丢失的法语和芬兰语字母)
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">cp866 - DOS 专用 Cyrillic 字符集
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">cp1251 - Windows 专用 Cyrillic 字符集
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">cp1252 - Windows 专用西欧字符集
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">KOI8-R - 俄语
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">BIG5 - 繁体中文,主要在台湾使用
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">GB2312 - 简体中文,国家标准字符集
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">BIG5-HKSCS - 带香港扩展的 Big5
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">Shift_JIS - 日语
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">EUC-JP - 日语
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">MacRoman - Mac 操作系统使用的字符集

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">ottom: 0px; font-weight: bold; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">注释:在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,170) 1px solid; padding-right: 15px">ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">double_encodeottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; line-height: 21px; padding-right: 0px">可选。布尔值,规定了是否编码已存在的 HTML 实体。

    ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 20px; border-left: 0px; padding-right: 0px">
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">TRUE - 认。将对每个实体进行转换。
  • disc; border-top: 0px; border-right: 0px; border-bottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">FALSE - 不会对已存在的 HTML 实体进行编码。

技术细节

ottom: rgb(170,170) 1px solid; padding-right: 15px">返回值:ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; line-height: 21px; padding-right: 0px">返回被转换的字符串。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">如果ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">string一个空的字符串,除非设置了 ENT_IGnorE 或者 ENT_SUBSTITUTE 标志。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(255,170) 1px solid; padding-right: 15px">PHP 版本:ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px; background-color: rgb(245,170) 1px solid; padding-right: 15px">更新日志:ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; line-height: 21px; padding-right: 0px">在 PHP 5 中,ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">character-set默认值改为 UTF-8。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">在 PHP 5.4 中,新增了:ENT_SUBSTITUTE、ENT_disALLOWED、ENT_HTML401、ENT_HTML5、ENT_XML1 和 ENT_XHTML。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">在 PHP 5.3 中,新增了 ENT_IGnorE。

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">在 PHP 5.2.3 中,新增了ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">double_encode

ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 12px 0px 0px; border-left: 0px; line-height: 21px; padding-right: 0px">在 PHP 4.1 中,新增了ottom: 0px; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; padding-right: 0px">character-set

更多实例

例子 1

把一些预定义的字符转换为 HTML 实体:

rush:PHP;"> PHP $str = "Bill & 'Steve'"; echo htmlspecialchars($str,ENT_COMPAT); // 只转换双引号 echo "
"; echo htmlspecialchars($str,ENT_QUOTES); // 转换双引号和单引号 echo "
"; echo htmlspecialchars($str,ENT_NOQUOTES); // 不转换任何引号 ?>

以上代码的 HTML 输出如下(查看源代码):

rush:PHP;"> Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

以上代码的浏览器输出

Bill & 'Steve' Bill & 'Steve' Bill & 'Steve'

运行实例

例子 2

把双引号转换为 HTML 实体:

rush:PHP;">

以上代码的 HTML 输出如下(查看源代码):

rush:PHP;"> I love "PHP".

以上代码的浏览器输出

I love "PHP".

下面看下PHP htmlspecialchars()的用法

htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。这个函数效果其实在浏览器中打开页面是看不到的,要查看源代码才能看到。

•& (和号) 成为 & •” (双引号) 成为 " •' (单引号) 成为 ' •< (小于) 成为 < •> (大于) 成为 >

htmlspecialchars(string,quotestyle,character-set)

quotestyle:

•ENT_COMPAT - 认。仅编码双引号。 •ENT_QUOTES - 编码双引号和单引号。 •ENT_NOQUOTES - 不编码任何引号。

总结

以上所述是小编给大家介绍的PHP htmlspecialchars() 函数实例代码用法大全,希望对大家有所帮助。程序员遇到问题都会上(编程之家jb51.cc)查找问题解答方法!如果觉得站点还不错,随手转发给程序员朋友一下!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用
将字符重新排列以形成回文(如果可能)在C++中
掌握PHP8底层开发原理和新特性:创建高效可扩展的应用程序
服务器性能优化必学:掌握PHP8底层开发原理
PHP8新特性和底层开发原理详解:优化应用性能的终极指南
将 C/C++ 代码转换为汇编语言
深入研究PHP8底层开发原理:创建高效可扩展的应用程序
C++程序查找法向量和迹
PHP8底层开发原理实战指南:提升服务器效能
重排数组,使得当 i 为偶数时,arr[i] >= arr[j],当 i 为奇数时,arr[i] <= arr[j],其中 j < i,使用 C++ 语言实现
Golang的垃圾回收:为什么它可以减少开发人员的负担?
C++程序:将一个数组的所有元素复制到另一个数组中
Golang:构建智能系统的基石
为什么AI开发者应该关注Golang?
在C和C++中,逗号(comma)的用法是用来分隔表达式或语句
PHP8底层开发原理解析及新特性应用实例
利用PHP8底层开发原理解析新特性:如何构建出色的Web应用