uTools-Manuals/docs/php/htmlentities.html
2019-04-08 23:22:26 +08:00

475 lines
15 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>将字符转换为 HTML 转义字符</title>
</head>
<body class="docs"><div id="layout">
<div id="layout-content"><div id="function.htmlentities" class="refentry">
<div class="refnamediv">
<h1 class="refname">htmlentities</h1>
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">htmlentities</span> &mdash; <span class="dc-title">将字符转换为 HTML 转义字符</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.htmlentities-description">
<h3 class="title">说明</h3>
<div class="methodsynopsis dc-description">
<span class="methodname"><strong>htmlentities</strong></span>
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span>
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = ENT_COMPAT | ENT_HTML401</span></span>
[, <span class="methodparam"><span class="type">string</span> <code class="parameter">$encoding</code><span class="initializer"> = ini_get(&quot;default_charset&quot;)</span></span>
[, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$double_encode</code><span class="initializer"> = true</span></span>
]]] ) : <span class="type">string</span></div>
<p class="para rdfs-comment">
本函数各方面都和 <span class="function"><a href="htmlspecialchars.html" class="function">htmlspecialchars()</a></span> 一样,
除了 <span class="function"><strong>htmlentities()</strong></span> 会转换所有具有 HTML 实体的字符。
</p>
<p class="para">
如果要解码(反向操作),可以使用 <span class="function"><a href="html_entity_decode.html" class="function">html_entity_decode()</a></span>
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.htmlentities-parameters">
<h3 class="title">参数</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">string</code></dt>
<dd>
<p class="para">
输入字符。
</p>
</dd>
<dt>
<code class="parameter">flags</code></dt>
<dd>
<p class="para">
以下一组位掩码标记,用于设置如何处理引号、无效代码序列、使用文档的类型。
默认是
<em>ENT_COMPAT | ENT_HTML401</em>
<table class="doctable table">
<caption><strong>有效 <code class="parameter">flags</code> 标记常量</strong></caption>
<thead>
<tr>
<th>常量名</th>
<th>描述</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td><strong><code>ENT_COMPAT</code></strong></td>
<td>会转换双引号,不转换单引号。</td>
</tr>
<tr>
<td><strong><code>ENT_QUOTES</code></strong></td>
<td>既转换双引号也转换单引号。</td>
</tr>
<tr>
<td><strong><code>ENT_NOQUOTES</code></strong></td>
<td>单/双引号都不转换</td>
</tr>
<tr>
<td><strong><code>ENT_IGNORE</code></strong></td>
<td>
静默丢弃无效的代码单元序列,而不是返回空字符串。
不建议使用此标记,
因为它<a href="http://unicode.org/reports/tr36/#Deletion_of_Noncharacters" class="link external">&raquo;&nbsp;可能有安全影响</a>
</td>
</tr>
<tr>
<td><strong><code>ENT_SUBSTITUTE</code></strong></td>
<td>
替换无效的代码单元序列为 Unicode 代替符Replacement Character
U+FFFD (UTF-8) 或者 &amp;#xFFFD; (其他),而不是返回空字符串。
</td>
</tr>
<tr>
<td><strong><code>ENT_DISALLOWED</code></strong></td>
<td>
为文档的无效代码点替换为 Unicode 代替符Replacement Character
U+FFFD (UTF-8),或 &amp;#xFFFD;(其他),而不是把它们留在原处。
比如以下情况下就很有用:要保证 XML 文档嵌入额外内容时格式合法。
</td>
</tr>
<tr>
<td><strong><code>ENT_HTML401</code></strong></td>
<td>
以 HTML 4.01 处理代码。
</td>
</tr>
<tr>
<td><strong><code>ENT_XML1</code></strong></td>
<td>
以 XML 1 处理代码。
</td>
</tr>
<tr>
<td><strong><code>ENT_XHTML</code></strong></td>
<td>
以 XHTML 处理代码。
</td>
</tr>
<tr>
<td><strong><code>ENT_HTML5</code></strong></td>
<td>
以 HTML 5 处理代码。
</td>
</tr>
</tbody>
</table>
</p>
</dd>
<dt>
<code class="parameter">encoding</code></dt>
<dd>
<p class="para">
An optional argument defining the encoding used when converting characters.
</p>
<p class="para">
If omitted, the default value of the <code class="parameter">encoding</code> varies
depending on the PHP version in use. In PHP 5.6 and later, the
<a href="ini.core.html#ini.default-charset" class="link">default_charset</a> configuration
option is used as the default value. PHP 5.4 and 5.5 will use
<em>UTF-8</em> as the default. Earlier versions of PHP use
<em>ISO-8859-1</em>.
</p>
<p class="para">
Although this argument is technically optional, you are highly encouraged to
specify the correct value for your code if you are using PHP 5.5 or earlier,
or if your <a href="ini.core.html#ini.default-charset" class="link">default_charset</a>
configuration option may be set incorrectly for the given input.
</p>
<p class="para">
支持以下字符集:
<table class="doctable table">
<caption><strong>支持的字符集列表</strong></caption>
<thead>
<tr>
<th>字符集</th>
<th>别名</th>
<th>描述</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td>ISO-8859-1</td>
<td>ISO8859-1</td>
<td>
西欧Latin-1
</td>
</tr>
<tr>
<td>ISO-8859-5</td>
<td>ISO8859-5</td>
<td>
Little used cyrillic charset (Latin/Cyrillic).
</td>
</tr>
<tr>
<td>ISO-8859-15</td>
<td>ISO8859-15</td>
<td>
西欧Latin-9。增加欧元符号法语和芬兰语字母在 Latin-1(ISO-8859-1) 中缺失。
</td>
</tr>
<tr>
<td>UTF-8</td>
<td class="empty">&nbsp;</td>
<td>
ASCII 兼容的多字节 8 位 Unicode。
</td>
</tr>
<tr>
<td>cp866</td>
<td>ibm866, 866</td>
<td>
DOS 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。
</td>
</tr>
<tr>
<td>cp1251</td>
<td>Windows-1251, win-1251, 1251</td>
<td>
Windows 特有的西里尔编码。本字符集在 4.3.2 版本中得到支持。
</td>
</tr>
<tr>
<td>cp1252</td>
<td>Windows-1252, 1252</td>
<td>
Windows 特有的西欧编码。
</td>
</tr>
<tr>
<td>KOI8-R</td>
<td>koi8-ru, koi8r</td>
<td>
俄语。本字符集在 4.3.2 版本中得到支持。
</td>
</tr>
<tr>
<td>BIG5</td>
<td>950</td>
<td>
繁体中文,主要用于中国台湾省。
</td>
</tr>
<tr>
<td>GB2312</td>
<td>936</td>
<td>
简体中文,中国国家标准字符集。
</td>
</tr>
<tr>
<td>BIG5-HKSCS</td>
<td class="empty">&nbsp;</td>
<td>
繁体中文,附带香港扩展的 Big5 字符集。
</td>
</tr>
<tr>
<td>Shift_JIS</td>
<td>SJIS, 932</td>
<td>
日语
</td>
</tr>
<tr>
<td>EUC-JP</td>
<td>EUCJP</td>
<td>
日语
</td>
</tr>
<tr>
<td>MacRoman</td>
<td class="empty">&nbsp;</td>
<td>
Mac OS 使用的字符串。
</td>
</tr>
<tr>
<td><em>&#039;&#039;</em></td>
<td class="empty">&nbsp;</td>
<td>
An empty string activates detection from script encoding (Zend multibyte),
<a href="ini.core.html#ini.default-charset" class="link">default_charset</a> and current
locale (see <span class="function"><a href="nl_langinfo.html" class="function">nl_langinfo()</a></span> and
<span class="function"><a href="setlocale.html" class="function">setlocale()</a></span>), in this order. Not recommended.
</td>
</tr>
</tbody>
</table>
<blockquote class="note"><p><strong class="note">Note</strong>:
<span class="simpara">
其他字符集没有认可。将会使用默认编码并抛出异常。
</span>
</p></blockquote>
</p>
</dd>
<dt>
<code class="parameter">double_encode</code></dt>
<dd>
<p class="para">
关闭 <code class="parameter">double_encode</code>PHP 不会转换现有的 HTML 实体,
默认是全部转换。
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.htmlentities-returnvalues">
<h3 class="title">返回值</h3>
<p class="para">
返回编码后的字符。
</p>
<p class="para">
如果指定的编码 <code class="parameter">encoding</code> 里,
<code class="parameter">string</code> 包含了无效的代码单元序列,
没有设置 <strong><code>ENT_IGNORE</code></strong> 或者
<strong><code>ENT_SUBSTITUTE</code></strong> 标记的情况下,会返回空字符串。
</p>
</div>
<div class="refsect1 changelog" id="refsect1-function.htmlentities-changelog">
<h3 class="title">更新日志</h3>
<p class="para">
<table class="doctable informaltable">
<thead>
<tr>
<th>版本</th>
<th>说明</th>
</tr>
</thead>
<tbody class="tbody">
<tr>
<td>5.6.0</td>
<td>
The default value for the <code class="parameter">encoding</code> parameter was
changed to be the value of the
<a href="ini.core.html#ini.default-charset" class="link">default_charset</a> configuration
option.
</td>
</tr>
<tr>
<td>5.4.0</td>
<td>
<code class="parameter">encoding</code> 参数的默认值改成 UTF-8。
</td>
</tr>
<tr>
<td>5.4.0</td>
<td>
增加常量 <strong><code>ENT_SUBSTITUTE</code></strong><strong><code>ENT_DISALLOWED</code></strong>
<strong><code>ENT_HTML401</code></strong><strong><code>ENT_XML1</code></strong>
<strong><code>ENT_XHTML</code></strong><strong><code>ENT_HTML5</code></strong>
</td>
</tr>
<tr>
<td>5.3.0</td>
<td>
增加常量 <strong><code>ENT_IGNORE</code></strong>
</td>
</tr>
<tr>
<td>5.2.3</td>
<td>
增加参数 <code class="parameter">double_encode</code>
</td>
</tr>
</tbody>
</table>
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.htmlentities-examples">
<h3 class="title">范例</h3>
<p class="para">
<div class="example" id="example-5911">
<p><strong>Example #1 <span class="function"><strong>htmlentities()</strong></span> 例子</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"A&nbsp;'quote'&nbsp;is&nbsp;&lt;b&gt;bold&lt;/b&gt;"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;输出:&nbsp;A&nbsp;'quote'&nbsp;is&nbsp;&amp;lt;b&amp;gt;bold&amp;lt;/b&amp;gt;<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;输出:&nbsp;A&nbsp;&amp;#039;quote&amp;#039;&nbsp;is&nbsp;&amp;lt;b&amp;gt;bold&amp;lt;/b&amp;gt;<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
<p class="para">
<div class="example" id="example-5912">
<p><strong>Example #2 <strong><code>ENT_IGNORE</code></strong> 用法示例</strong></p>
<div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$str&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"\x8F!!!"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;输出空&nbsp;string<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UTF-8"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;输出&nbsp;"!!!"<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">ENT_QUOTES&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">ENT_IGNORE</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"UTF-8"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.htmlentities-seealso">
<h3 class="title">参见</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="html_entity_decode.html" class="function" rel="rdfs-seeAlso">html_entity_decode()</a> - Convert HTML entities to their corresponding characters</span></li>
<li class="member"><span class="function"><a href="get_html_translation_table.html" class="function" rel="rdfs-seeAlso">get_html_translation_table()</a> - 返回使用 htmlspecialchars 和 htmlentities 后的转换表</span></li>
<li class="member"><span class="function"><a href="htmlspecialchars.html" class="function" rel="rdfs-seeAlso">htmlspecialchars()</a> - 将特殊字符转换为 HTML 实体</span></li>
<li class="member"><span class="function"><a href="nl2br.html" class="function" rel="rdfs-seeAlso">nl2br()</a> - 在字符串所有新行之前插入 HTML 换行标记</span></li>
<li class="member"><span class="function"><a href="urlencode.html" class="function" rel="rdfs-seeAlso">urlencode()</a> - 编码 URL 字符串</span></li>
</ul>
</p>
</div>
</div></div></div></body></html>