<?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 => 'ja',
  ),
  'this' => 
  array (
    0 => 'function.htmlspecialchars.php',
    1 => 'htmlspecialchars',
    2 => '特殊文字を HTML エンティティに変換する',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String 関数',
  ),
  'prev' => 
  array (
    0 => 'function.htmlentities.php',
    1 => 'htmlentities',
  ),
  'next' => 
  array (
    0 => 'function.htmlspecialchars-decode.php',
    1 => 'htmlspecialchars_decode',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    '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> (アンパサンド)</td>
       <td><code class="literal">&amp;amp;</code></td>
      </tr>

      <tr>
       <td><code class="literal">&quot;</code> (ダブルクォート)</td>
       <td><strong><code><a href="string.constants.php#constant.ent-noquotes">ENT_NOQUOTES</a></code></strong> が指定されていない場合、<code class="literal">&amp;quot;</code></td>
      </tr>

      <tr>
       <td><code class="literal">&#039;</code> (シングルクォート)</td>
       <td>
        <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> の場合)。ただし
        <strong><code><a href="string.constants.php#constant.ent-quotes">ENT_QUOTES</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">
       変換される文字列。
      </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 の置換文字に置き換えます。
            UTF-8 の場合は U+FFFD、それ以外の場合は &amp;#xFFFD; となります。
           </td>
          </tr>

          <tr>
           <td><strong><code><a href="string.constants.php#constant.ent-disallowed">ENT_DISALLOWED</a></code></strong></td>
           <td>
            指定した文書型において無効な符号位置を、Unicode の代替文字である
            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">
  オプションの引数。文字を変換するときに使うエンコーディングを定義します。
 </p>

 <p class="para">
  省略した場合の <code class="parameter">encoding</code> のデフォルト値は、
  <a href="ini.core.php#ini.default-charset" class="link">default_charset</a> の値を使います。
 </p>

 <p class="para">
  技術的にはこの引数を省略可能ですが、
  <a href="ini.core.php#ini.default-charset" class="link">default_charset</a>
  の指定が入力とは違う文字セットになっている可能性もあるので、
  適切な値を指定しておくことを強く推奨します。
 </p>

      <p class="para">
       この関数を使ううえでは
       <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> は事実上同等です。
       <code class="parameter">string</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>
      ほとんど使われないキリル文字セット (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 固有のキリル文字セット。
     </td>
    </tr>

    <tr>
     <td>cp1251</td>
     <td>Windows-1251, win-1251, 1251</td>
     <td>
      Windows 固有のキリル文字セット。
     </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>
      ロシア語。
     </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, SJIS-win, cp932, 932</td>
     <td>
      日本語。
     </td>
    </tr>

    <tr>
     <td>EUC-JP</td>
     <td>EUCJP, eucJP-win</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>
      空文字列を指定すると、
      スクリプトのエンコーディング (Zend multibyte)、
      <a href="ini.core.php#ini.default-charset" class="link">default_charset</a>、
      そして現在のロケール (<span class="function"><a href="function.nl-langinfo.php" class="function">nl_langinfo()</a></span> および
      <span class="function"><a href="function.setlocale.php" class="function">setlocale()</a></span> を参照ください)
      の順でエンコーディングを検出します。
      この方法はおすすめしません。
     </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">
   変換後の文字列を返します。
  </p>
  <p class="para">
   入力の <code class="parameter">string</code> の中に
   エンコーディング <code class="parameter">encoding</code> における無効なコードユニットシーケンスが含まれており、
   かつ <strong><code><a href="string.constants.php#constant.ent-ignore">ENT_IGNORE</a></code></strong> フラグが設定されていなければ、
   <span class="function"><strong>htmlspecialchars()</strong></span> は空文字列を返します。
  </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); ?>