PHP htmlspecialchars() 函数
❮ PHP String 字符串参考手册
实例
把预定义的字符 "<" (小于)和 ">" (大于)转换为 HTML 实体:
<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>
以上代码的 HTML 输出如下(查看源代码):
<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
以上代码的浏览器输出:
This is some <b>bold</b> text.
亲自试一试 »
定义和用法
htmlspecialchars() 函数把预定义的字符转换为 HTML 实体。
预定义的字符是:
- & (和号)成为 &
- " (双引号)成为 "
- ' (单引号)成为 '
- < (小于)成为 <
- > (大于)成为 >
提示:如需把特殊的 HTML 实体转换回字符,请使用 htmlspecialchars_decode() 函数。
语法
htmlspecialchars(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">
<?php<br>
$str = "Jane & 'Tarzan'";<br>
echo htmlspecialchars($str, ENT_COMPAT); // Will only convert double quotes<br>
echo "<br>";<br>
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes<br>
echo "<br>";<br>
echo htmlspecialchars($str, ENT_NOQUOTES); // Does not convert any quotes<br>
?></div>
<p>以上代码的 HTML 输出如下(查看源代码):</p>
<div class="w3-code notranslate htmlHigh">
<!DOCTYPE html><br><html><br>
<body><br>
Jane &amp; 'Tarzan'<br><br>
Jane &amp; &#039;Tarzan&#039;<br><br>
Jane &amp; 'Tarzan'<br>
</body><br>
</html>
</div>
<p>以上代码的浏览器输出:</p>
<div class="w3-code notranslate htmlHigh">
Jane & 'Tarzan'<br>
Jane & 'Tarzan'<br>
Jane & 'Tarzan'
</div>
<a target="_blank" class="w3-btn w3-margin-bottom" href="/code-document/php/phptryit.asp?filename=tryphp_func_string_htmlspecialchars2">亲自试一试 »</a>
</div>
<div class="w3-example">
<h3>实例</h3>
<p>把双引号转换为 HTML 实体:</p>
<div class="w3-code notranslate htmlHigh">
<?php<br>
$str = 'I love "PHP".';<br>echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes<br>
?></div>
<p>以上代码的 HTML 输出如下(查看源代码):</p>
<div class="w3-code notranslate htmlHigh">
<!DOCTYPE html><br><html><br>
<body><br>
I love &quot;PHP&quot;.<br>
</body><br>
</html>
</div>
<p>以上代码的浏览器输出:</p>
<div class="w3-code notranslate htmlHigh">
I love "PHP".</div>
<a target="_blank" class="w3-btn w3-margin-bottom" href="/code-document/php/phptryit.asp?filename=tryphp_func_string_htmlspecialchars3">亲自试一试 »</a>
</div>
<hr/>
<a href="/code-document/php/php_ref_string.html" class="w3-btn w3-white w3-border" title="PHP 字符串参考">❮ PHP String 字符串参考手册</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">×</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_htmlspecialchars.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">×</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>
|