PHP 教程

PHP 教程 PHP 简介 PHP 下载安装和环境配置 PHP 语法 PHP 注释 PHP 变量 PHP Echo / Print PHP 数据类型 PHP 字符串 PHP 数值 PHP Math PHP 常量 PHP 运算符 PHP If...Else...Elseif PHP Switch PHP 循环 PHP 函数 PHP 数组 PHP 全局变量 PHP 正则表达式

PHP 表单

PHP 表单处理 PHP 表单验证 PHP 表单验证必填字段 PHP 表单验证URL/E-mail PHP 表单验证实例

PHP 高级教程

PHP 日期和时间 PHP Include PHP 文件处理 PHP 文件打开/读取 PHP 文件创建/写入 PHP 文件上传 PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP 回调函数 PHP JSON PHP 异常处理

PHP 面向对象

PHP 什么是 OOP PHP 类和对象 PHP 构造函数 PHP 析构函数 PHP 访问修饰符 PHP 继承 PHP 类常量 PHP 抽象类 PHP 接口 PHP 特征 PHP 静态方法 PHP 静态属性 PHP 命名空间 PHP 可迭代对象

MySQL 数据库

MySQL Database MySQL Connect MySQL Create DB MySQL Create Table MySQL Insert Data MySQL Get Last ID MySQL Insert Multiple MySQL Prepared MySQL Select Data MySQL Where MySQL Order By MySQL Delete Data MySQL Update Data MySQL Limit Data

PHP XML

PHP XML Parsers PHP SimpleXML Parser PHP SimpleXML - Get PHP XML Expat PHP XML DOM

PHP - AJAX

AJAX Intro AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX Poll

PHP 实例

PHP 实例 PHP 编译器 PHP 测验 PHP 练习

PHP 参考手册

PHP 概述 PHP Array PHP Calendar PHP Date PHP Directory PHP Error PHP Exception PHP Filesystem PHP Filter PHP FTP PHP JSON PHP Keywords PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP Network PHP Output Control PHP RegEx PHP SimpleXML PHP Stream PHP String PHP Variable Handling PHP XML Parser PHP Zip PHP Timezones

PHP htmlentities() 函数

❮ PHP String Reference

实例

把字符转换为 HTML 实体:

<?php
$str = '<a href="">Go to www.</a>';
echo htmlentities($str);
?>

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

&lt;a href=&quot;&quot;&gt;Go to www.&lt;/a&gt;

以上代码的浏览器输出:

<a href="">Go to www.</a>
亲自试一试 »

定义和用法

htmlentities() 函数把字符转换为 HTML 实体。

提示:要把 HTML 实体转换回字符,请使用 html_entity_decode() 函数。

提示:请使用 get_html_translation_table() 函数来返回 htmlentities() 使用的翻译表。


语法

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

参数值

参数 描述
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;。</li> </ul> <p>规定使用的文档类型的附加 flags:</p> <ul class="listintable"> <li>ENT_HTML401 - 默认。作为 HTML 4.01 处理代码。</li> <li>ENT_HTML5 - 作为 HTML 5 处理代码。</li> <li>ENT_XML1 - 作为 XML 1 处理代码。</li> <li>ENT_XHTML - 作为 XHTML 处理代码。</li> </ul> </td> </tr> <tr> <td><i>character-set</i></td> <td> <p>可选。一个规定了要使用的字符集的字符串。</p> <p>允许的值:</p> <ul class="listintable"> <li>UTF-8 - 默认。ASCII 兼容多字节的 8 位 Unicode</li> <li>ISO-8859-1 - 西欧</li> <li>ISO-8859-15 - 西欧(加入欧元符号 + ISO-8859-1 中丢失的法语和芬兰语字母)</li> <li>cp866 - DOS 专用 Cyrillic 字符集</li> <li>cp1251 - Windows 专用 Cyrillic 字符集</li> <li>cp1252 - Windows 专用西欧字符集</li> <li>KOI8-R - 俄语</li> <li>BIG5 - 繁体中文,主要在中国台湾使用</li> <li>GB2312 - 简体中文,国家标准字符集</li> <li>BIG5-HKSCS - 中国香港扩展的 Big5</li> <li>Shift_JIS - 日语</li> <li>EUC-JP - 日语</li> <li>MacRoman - Mac 操作系统使用的字符集</li> </ul> <p class="note"><span>注释:</span>在 PHP 5.4 之前的版本,无法被识别的字符集将被忽略并由 ISO-8859-1 替代。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 替代。</p> </td> </tr> <tr> <td><i>double_encode</i></td> <td> <p>可选。布尔值,规定是否编码已存在的 HTML 实体。</p> <ul class="listintable"> <li>TRUE - 默认。将对每个实体进行转换。</li> <li>FALSE - 不会对已存在的 HTML 实体进行编码。</li> </ul> </td> </tr> </table> <hr/> <h2>技术细节</h2> <table class="w3-table-all"> <tr> <td style="width:20%;">返回值:</td> <td> <p>返回被转换的字符串。</p> <p>如果 <i>string</i> 包含无效的编码,则返回一个空的字符串,除非设置了 ENT_IGNORE 或者 ENT_SUBSTITUTE 标志。</p> </td> </tr> <tr> <td>PHP 版本:</td> <td>4+</td> </tr> <tr> <td>更新日志:</td> <td> <p>在 PHP 5 中,<i>character-set</i> 参数的默认值改为 UTF-8。</p> <p>在 PHP 5.4 中,新增了:ENT_SUBSTITUTE、ENT_DISALLOWED、ENT_HTML401、ENT_HTML5、ENT_XML1 和 ENT_XHTML。</p> <p>在 PHP 5.3 中,新增了 ENT_IGNORE。</p> <p>在 PHP 5.2.3 中,新增了 <i>double_encode</i> 参数。</p> <p>在 PHP 4.1 中,新增了 <i>character-set</i> 参数。</p> </td> </tr> </table> <hr/> <h2>更多实例</h2> <div class="w3-example"> <h3>实例</h3> <p>把字符转换为 HTML 实体:</p> <div class="w3-code notranslate htmlHigh"> &lt;?php<br> $str = "Albert Einstein said: 'E=MC²'";<br> echo htmlentities($str, ENT_COMPAT); // Will only convert double quotes<br> echo "&lt;br&gt;";<br> echo htmlentities($str, ENT_QUOTES); // Converts double and single quotes<br> echo "&lt;br&gt;";<br> echo htmlentities($str, ENT_NOQUOTES); // Does not convert any quotes<br> ?&gt;</div> <p>以上代码的 HTML 输出如下(查看源代码):</p> <div class="w3-code notranslate htmlHigh"> Albert Einstein said: 'E=MC&amp;sup2;'&lt;br&gt;<br>Albert Einstein said: &amp;#039;E=MC&amp;sup2;&amp;#039;&lt;br&gt;<br>Albert Einstein said: 'E=MC&amp;sup2;' </div> <p>以上代码的浏览器输出:</p> <div class="w3-code notranslate htmlHigh"> Albert Einstein said: 'E=MC²'<br>Albert Einstein said: 'E=MC²'<br>Albert Einstein said: 'E=MC²' </div> <a target="_blank" class="w3-btn w3-margin-bottom" href="/code-document/php/phptryit.asp?filename=tryphp_func_string_htmlentities2"> 亲自试一试 &raquo;</a> </div> <div class="w3-example"> <h3>实例</h3> <p>通过使用西欧字符集,把一些字符转换为 HTML 实体:</p> <div class="w3-code notranslate htmlHigh"> &lt;?php<br>$str = "My name is Øyvind Åsane. I'm Norwegian.";<br>echo htmlentities($str, ENT_QUOTES, "UTF-8"); // Will only convert double quotes (not single quotes), and uses the character-set Western European<br>?&gt;</div> <p>以上代码的 HTML 输出如下(查看源代码):</p> <div class="w3-code notranslate htmlHigh"> &lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>&lt;body&gt;<br>My name is &amp;Oslash;yvind &amp;Aring;sane. I&amp;#039;m Norwegian.<br>&lt;/body&gt;<br>&lt;/html&gt; </div> <p>以上代码的浏览器输出:</p> <div class="w3-code notranslate htmlHigh"> My name is Øyvind Åsane. I'm Norwegian.</div> <a target="_blank" class="w3-btn w3-margin-bottom" href="/code-document/php/phptryit.asp?filename=tryphp_func_string_htmlentities3"> 亲自试一试 &raquo;</a> </div> <hr/> <a href="/code-document/php/php_ref_string.html" class="w3-btn w3-white w3-border" title="PHP String 参考">&#10094; PHP String Reference</a> </div> <div class="w3-col l2 m12 w3-hide-medium" id="right"> <div class="section-adv" style="margin-left:auto;margin-right:auto;"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-2533243013167625" crossorigin="anonymous"></script> <!-- G:336:280 --> <ins class="adsbygoogle" style="display:inline-block;width:336px;height:280px" data-ad-client="ca-pub-2533243013167625" data-ad-slot="2941966644"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="sidesection"> <h4><a href="/code-document/colors/colors-picker.html">颜色选择器</a></h4> <a href="/code-document/colors/colors-picker.html"><img src="/images/colorpicker.gif" alt="颜色选择器"></a> </div> <div class="sidesection" style="margin-left:auto;margin-right:auto;max-width:250px"> <h4><a class="w3-button w3-green w3-block w3-round" href="/code-document/php/php-ref-overview.asp" target="_blank" style="text-decoration:none;margin-top:1px;margin-bottom:1px">PHP 参考手册</a></h4> <h4><a class="w3-button w3-blue w3-block w3-round" href="/code-document/php/php-examples.asp" target="_blank" style="text-decoration:none;margin-top:1px;margin-bottom:1px">PHP 实例</a></h4> <h4><a class="w3-button w3-pink w3-block w3-round" href="/code-document/quiztest/quiztest.asp?qtest=PHP" target="_blank" style="text-decoration:none;margin-top:1px;margin-bottom:1px">PHP 测验/考试</a></h4> </div> <div class="sidesection"> <h4>读后有收获微信请站长喝咖啡</h4> <img src="/images/weixinpay.png" alt="weixinpay"> </div> </div> </div> <div id="footer" class="footer w3-container w3-white"> <div id="adngin-bottom_left-1"></div> <div class="w3-row-padding w3-center w3-small w3-hide-small" style="margin:0 -16px"> <div class="w3-col l3 m3 s12"> <a class="w3-button w3-light-grey w3-block" href="javascript:void(0);" onclick="displayError();return false" style="white-space:nowrap;text-decoration:none;margin-top:1px;margin-bottom:1px">报告错误</a> </div> <div class="w3-col l3 m3 s12"> <a class="w3-button w3-light-grey w3-block" href="javascript:void(0);" target="_blank" onclick="printPage();return false;" style="text-decoration:none;margin-top:1px;margin-bottom:1px">打印</a> </div> <div class="w3-col l3 m3 s12"> <a class="w3-button w3-light-grey w3-block" href="/code-document/about/default.html" target="_top" style="text-decoration:none;margin-top:1px;margin-bottom:1px">关于</a> </div> <div class="w3-col l3 m3 s12"> <a class="w3-button w3-light-grey w3-block" href="/code-document/about/about_learnpath.html" target="_top" style="text-decoration:none;margin-top:1px;margin-bottom:1px">学习路线</a> </div> </div> <div class="w3-light-grey w3-padding w3-margin-bottom w3-hide-small" id="err_form" style="display:none;position:relative"> <span onclick="this.parentElement.style.display='none'" class="w3-button w3-display-topright w3-large">&times;</span> <h2>错误报告</h2> <p>如果您发现内容有误或提出修改建议,请随时向我们发送 E-mail 邮件:</p> <p></p> <h2>您的建议:</h2> <form> <div class="w3-section"> <label for="err_email">您的 E-mail:</label> <input class="w3-input w3-border" type="text" style="margin-top:5px;width:100%" id="err_email" name="err_email"> </div> <div class="w3-section"> <label for="err_email">页面地址:</label> <input class="w3-input w3-border" type="text" style="width:100%;margin-top:5px" id="err_url" name="err_url" value="//php/func_string_htmlentities.asp" disabled="disabled"> </div> <div class="w3-section"> <label for="err_email">内容描述:</label> <textarea rows="10" class="w3-input w3-border" id="err_desc" name="err_desc" style="width:100%;margin-top:5px;resize:vertical;"></textarea> </div> <div class="form-group"> <button type="button" class="w3-button w3-dark-grey" onclick="sendErr()">提交</button> </div> </form> </div> <div class="w3-container w3-light-grey w3-padding w3-hide-small" id="err_sent" style="display:none;position:relative"> <span onclick="this.parentElement.style.display='none'" class="w3-button w3-display-topright">&times;</span> <h2>感谢您的帮助!</h2> <p>您的建议已发送到 W3schools。</p> </div> <div class="w3-center w3-small w3-opacity" style="margin-top:20px;"> <br> <a href="/code-document/about/default.html">关于我们</a> | <a href="/code-document/about/about_privacy.html">隐私条款</a> | <a href="/code-document/about/about_learnpath.html">学习路线</a> | <a href="https://beian.miit.gov.cn/" rel="nofollow" target="_blank">京ICP备14045893号-9</a> </div> <div class="mod-goback"> <a href="#" rel="nofollow">返回顶部</a> </div> </div> <!-- BEGIN TONGJI --> <div style="display:none"> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?84f10bfafb317df93b045931df893d63"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </div> <!-- END TONGJI --> </div> <script src="/lib/w3schools_footer.js"></script> </body> </html>