<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.strings.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.htmlspecialchars.php',
    1 => 'htmlspecialchars',
    2 => '将特殊字符转换为 HTML 实体',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => '字符串 函数',
  ),
  'prev' => 
  array (
    0 => 'function.htmlentities.php',
    1 => 'htmlentities',
  ),
  'next' => 
  array (
    0 => 'function.htmlspecialchars-decode.php',
    1 => 'htmlspecialchars_decode',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/strings/functions/htmlspecialchars.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.htmlspecialchars" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">htmlspecialchars</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">htmlspecialchars</span> &mdash; <span class="dc-title">将特殊字符转换为 HTML 实体</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.htmlspecialchars-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>htmlspecialchars</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$string</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$flags</code><span class="initializer"> = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401</span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><span class="type"><a href="language.types.null.php" class="type null">?</a></span><span class="type"><a href="language.types.string.php" class="type string">string</a></span></span> <code class="parameter">$encoding</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.null">null</a></code></strong></span></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.boolean.php" class="type bool">bool</a></span> <code class="parameter">$double_encode</code><span class="initializer"> = <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong></span></span><br>): <span class="type"><a href="language.types.string.php" class="type string">string</a></span></div>

  <p class="para rdfs-comment">
   某类字符在 HTML 中有特殊用处，如需保持原意，需要用 HTML 实体来表达。
   本函数会返回字符转义后的表达。
   如需转换子字符串中所有关联的名称实体，使用 <span class="function"><a href="function.htmlentities.php" class="function">htmlentities()</a></span> 代替本函数。
  </p>
  <p class="para">
   如果传入字符的字符编码和最终的文档是一致的，则用函数处理的输入适合绝大多数 HTML 文档环境。
   然而，如果输入的字符编码和最终包含字符的文档是不一样的，
   想要保留字符（以数字或名称实体的形式），本函数以及 <span class="function"><a href="function.htmlentities.php" class="function">htmlentities()</a></span> 
   （仅编码名称实体对应的子字符串）可能不够用。
   这种情况可以使用 <span class="function"><a href="function.mb-encode-numericentity.php" class="function">mb_encode_numericentity()</a></span> 代替。
  </p>
  <p class="para">
   <table class="doctable table">
    <caption><strong>执行转换</strong></caption>
    
     <thead>
      <tr>
       <th>字符</th>
       <th>替换后</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td><code class="literal">&amp;</code> (&amp; 符号)</td>
       <td><code class="literal">&amp;amp;</code></td>
      </tr>

      <tr>
       <td><code class="literal">&quot;</code> (双引号)</td>
       <td><code class="literal">&amp;quot;</code>，除非设置了 <strong><code><a href="string.constants.php#constant.ent-noquotes">ENT_NOQUOTES</a></code></strong></td>
      </tr>

      <tr>
       <td><code class="literal">&#039;</code> (单引号)</td>
       <td>
         设置了 <strong><code><a href="string.constants.php#constant.ent-quotes">ENT_QUOTES</a></code></strong> 后，
        <code class="literal">&amp;#039;</code>
        (如果是 <strong><code><a href="string.constants.php#constant.ent-html401">ENT_HTML401</a></code></strong>) ，或者 <code class="literal">&amp;apos;</code> (如果是
        <strong><code><a href="string.constants.php#constant.ent-xml1">ENT_XML1</a></code></strong>、 <strong><code><a href="string.constants.php#constant.ent-xhtml">ENT_XHTML</a></code></strong> 或
        <strong><code><a href="string.constants.php#constant.ent-html5">ENT_HTML5</a></code></strong>)。
       </td>
      </tr>

      <tr>
       <td><code class="literal">&lt;</code> (小于)</td>
       <td><code class="literal">&amp;lt;</code></td>
      </tr>

      <tr>
       <td><code class="literal">&gt;</code> (大于)</td>
       <td><code class="literal">&amp;gt;</code></td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.htmlspecialchars-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">string</code></dt>
     <dd>
      <p class="para">
       待转换的 <span class="type"><a href="language.types.string.php" class="type string">string</a></span>。
      </p>
     </dd>
    
    
     <dt><code class="parameter">flags</code></dt>
     <dd>
      <p class="para">
       位掩码，由以下某个或多个标记组成，设置转义处理细节、无效单元序列、文档类型。
       默认是
       <code class="literal">ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401</code>。
       <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><a href="string.constants.php#constant.ent-compat">ENT_COMPAT</a></code></strong></td>
           <td>会转换双引号，不转换单引号。</td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-quotes">ENT_QUOTES</a></code></strong></td>
           <td>既转换双引号也转换单引号。</td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-noquotes">ENT_NOQUOTES</a></code></strong></td>
           <td>单/双引号都不转换</td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-ignore">ENT_IGNORE</a></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><a href="string.constants.php#constant.ent-substitute">ENT_SUBSTITUTE</a></code></strong></td>
           <td>
            替换无效的代码单元序列为 Unicode 代替符（Replacement Character），
            U+FFFD (UTF-8) 或者 &amp;#xFFFD; (其他)，而不是返回空字符串。
           </td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-disallowed">ENT_DISALLOWED</a></code></strong></td>
           <td>
            为文档的无效代码点替换为  Unicode 代替符（Replacement Character）：
            U+FFFD (UTF-8)，或 &amp;#xFFFD;（其他），而不是把它们留在原处。
            比如以下情况下就很有用：要保证 XML 文档嵌入额外内容时格式合法。
           </td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-html401">ENT_HTML401</a></code></strong></td>
           <td>
            以 HTML 4.01 处理代码。
           </td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-xml1">ENT_XML1</a></code></strong></td>
           <td>
            以 XML 1 处理代码。
           </td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-xhtml">ENT_XHTML</a></code></strong></td>
           <td>
            以 XHTML 处理代码。
           </td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-html5">ENT_HTML5</a></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, <code class="parameter">encoding</code> defaults to the value of the
  <a href="ini.core.php#ini.default-charset" class="link">default_charset</a> configuration
  option.
 </p>

 <p class="para">
  Although this argument is technically optional, you are highly encouraged to
  specify the correct value for your code
  if the <a href="ini.core.php#ini.default-charset" class="link">default_charset</a>
  configuration option may be set incorrectly for the given input.
 </p>

      <p class="para">
       本函数使用效果上，如果 <code class="parameter">string</code> 对以下字符编码是有效的，
       <code class="literal">ISO-8859-1</code>、 <code class="literal">ISO-8859-15</code>、
       <code class="literal">UTF-8</code>、 <code class="literal">cp866</code>、
       <code class="literal">cp1251</code>、 <code class="literal">cp1252</code>、
       <code class="literal">KOI8-R</code> 将具有相同的效果。
       也就是说，在这些编码里，
       受 <span class="function"><strong>htmlspecialchars()</strong></span> 影响的字符会占据相同的位置。
      </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><code class="literal">&#039;&#039;</code></td>
     <td class="empty">&nbsp;</td>
     <td>
      An empty string activates detection from script encoding (Zend multibyte),
      <a href="ini.core.php#ini.default-charset" class="link">default_charset</a> and current
      locale (see <span class="function"><a href="function.nl-langinfo.php" class="function">nl_langinfo()</a></span> and
      <span class="function"><a href="function.setlocale.php" class="function">setlocale()</a></span>), in this order. Not recommended.
     </td>
    </tr>

   </tbody>
  
 </table>

 <blockquote class="note"><p><strong class="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.htmlspecialchars-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   转换后的 <span class="type"><a href="language.types.string.php" class="type string">string</a></span>。
  </p>
  <p class="para">
   如果指定的编码 <code class="parameter">encoding</code> 里，
   <code class="parameter">string</code> 包含了无效的代码单元序列，
   没有设置 <strong><code><a href="string.constants.php#constant.ent-ignore">ENT_IGNORE</a></code></strong> 或者
   <strong><code><a href="string.constants.php#constant.ent-substitute">ENT_SUBSTITUTE</a></code></strong> 标记的情况下，会返回空字符串。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.htmlspecialchars-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.1.0</td>
      <td>
       <code class="parameter">flags</code> 从 <strong><code><a href="string.constants.php#constant.ent-compat">ENT_COMPAT</a></code></strong> 变更为 <strong><code><a href="string.constants.php#constant.ent-quotes">ENT_QUOTES</a></code></strong> | <strong><code><a href="string.constants.php#constant.ent-substitute">ENT_SUBSTITUTE</a></code></strong> | <strong><code><a href="string.constants.php#constant.ent-html401">ENT_HTML401</a></code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.htmlspecialchars-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>htmlspecialchars()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$new </span><span style="color: #007700">= </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #DD0000">"&lt;a href='test'&gt;Test&lt;/a&gt;"</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$new</span><span style="color: #007700">; </span><span style="color: #FF8000">// &amp;lt;a href=&amp;#039;test&amp;#039;&amp;gt;Test&amp;lt;/a&amp;gt;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.htmlspecialchars-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    注意，本函数不会转换以上列表以外的实体。
    完整转换请参见 <span class="function"><a href="function.htmlentities.php" class="function">htmlentities()</a></span>。
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    如果 <code class="parameter">flags</code> 的设置模糊易混淆，将遵循以下规则：
   </p>
   <p class="para">
    <ul class="itemizedlist">
     <li class="listitem">
      <span class="simpara">
       当 <strong><code><a href="string.constants.php#constant.ent-compat">ENT_COMPAT</a></code></strong>、<strong><code><a href="string.constants.php#constant.ent-quotes">ENT_QUOTES</a></code></strong>、<strong><code><a href="string.constants.php#constant.ent-noquotes">ENT_NOQUOTES</a></code></strong> 都没设置，
       默认就是 <strong><code><a href="string.constants.php#constant.ent-noquotes">ENT_NOQUOTES</a></code></strong>。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       如果设置不止一个 <strong><code><a href="string.constants.php#constant.ent-compat">ENT_COMPAT</a></code></strong>、 <strong><code><a href="string.constants.php#constant.ent-quotes">ENT_QUOTES</a></code></strong>、
       <strong><code><a href="string.constants.php#constant.ent-noquotes">ENT_NOQUOTES</a></code></strong> ，优先级最高的是 <strong><code><a href="string.constants.php#constant.ent-quotes">ENT_QUOTES</a></code></strong>，
       其次是 <strong><code><a href="string.constants.php#constant.ent-compat">ENT_COMPAT</a></code></strong>。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       当 <strong><code><a href="string.constants.php#constant.ent-html401">ENT_HTML401</a></code></strong>、 <strong><code><a href="string.constants.php#constant.ent-html5">ENT_HTML5</a></code></strong>、
       <strong><code><a href="string.constants.php#constant.ent-xhtml">ENT_XHTML</a></code></strong>、 <strong><code><a href="string.constants.php#constant.ent-xml1">ENT_XML1</a></code></strong> 都没设置，默认是
       <strong><code><a href="string.constants.php#constant.ent-html401">ENT_HTML401</a></code></strong>。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       如果设置不止一个 <strong><code><a href="string.constants.php#constant.ent-html401">ENT_HTML401</a></code></strong>、 <strong><code><a href="string.constants.php#constant.ent-html5">ENT_HTML5</a></code></strong>、
       <strong><code><a href="string.constants.php#constant.ent-xhtml">ENT_XHTML</a></code></strong>、 <strong><code><a href="string.constants.php#constant.ent-xml1">ENT_XML1</a></code></strong>，
       优先级最高的是
       <strong><code><a href="string.constants.php#constant.ent-html5">ENT_HTML5</a></code></strong> 其次是 <strong><code><a href="string.constants.php#constant.ent-xhtml">ENT_XHTML</a></code></strong>、<strong><code><a href="string.constants.php#constant.ent-xml1">ENT_XML1</a></code></strong> 和 <strong><code><a href="string.constants.php#constant.ent-html401">ENT_HTML401</a></code></strong>。
      </span>
     </li>
     <li class="listitem">
      <span class="simpara">
       如果设置不止一个 <strong><code><a href="string.constants.php#constant.ent-disallowed">ENT_DISALLOWED</a></code></strong>、 <strong><code><a href="string.constants.php#constant.ent-ignore">ENT_IGNORE</a></code></strong>、
       <strong><code><a href="string.constants.php#constant.ent-substitute">ENT_SUBSTITUTE</a></code></strong>，优先级最高的是 <strong><code><a href="string.constants.php#constant.ent-ignore">ENT_IGNORE</a></code></strong>，
       其次是 <strong><code><a href="string.constants.php#constant.ent-substitute">ENT_SUBSTITUTE</a></code></strong>。
      </span>
     </li>
    </ul>
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.htmlspecialchars-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.get-html-translation-table.php" class="function" rel="rdfs-seeAlso">get_html_translation_table()</a> - 返回使用 htmlspecialchars 和 htmlentities 后的转换表</span></li>
    <li><span class="function"><a href="function.htmlspecialchars-decode.php" class="function" rel="rdfs-seeAlso">htmlspecialchars_decode()</a> - 将特殊的 HTML 实体转换回普通字符</span></li>
    <li><span class="function"><a href="function.strip-tags.php" class="function" rel="rdfs-seeAlso">strip_tags()</a> - 从字符串中去除 HTML 和 PHP 标签</span></li>
    <li><span class="function"><a href="function.htmlentities.php" class="function" rel="rdfs-seeAlso">htmlentities()</a> - 将字符转换为 HTML 转义字符</span></li>
    <li><span class="function"><a href="function.nl2br.php" class="function" rel="rdfs-seeAlso">nl2br()</a> - 在字符串所有新行之前插入 HTML 换行标记</span></li>
   </ul>
  </p>
 </div>


</div><?php manual_footer($setup); ?>