<?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.str-getcsv.php',
    1 => 'str_getcsv',
    2 => 'CSV 文字列をパースして配列に格納する',
  ),
  'up' => 
  array (
    0 => 'ref.strings.php',
    1 => 'String 関数',
  ),
  'prev' => 
  array (
    0 => 'function.str-ends-with.php',
    1 => 'str_ends_with',
  ),
  'next' => 
  array (
    0 => 'function.str-increment.php',
    1 => 'str_increment',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'ja',
    'path' => 'reference/strings/functions/str-getcsv.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.str-getcsv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">str_getcsv</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">str_getcsv</span> &mdash; <span class="dc-title">
   CSV 文字列をパースして配列に格納する
  </span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.str-getcsv-description">
  <h3 class="title">説明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>str_getcsv</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.string.php" class="type string">string</a></span> <code class="parameter">$separator</code><span class="initializer"> = &quot;,&quot;</span></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">$enclosure</code><span class="initializer"> = &quot;\&quot;&quot;</span></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">$escape</code><span class="initializer"> = &quot;\\&quot;</span></span><br>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="para rdfs-comment">
   <abbr title="Comma Separated Value">CSV</abbr> 形式の文字列入力のフィールドをパースして、
   読み込んだフィールドの内容を配列で返します。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    この関数はロケール設定を考慮します。
    例えば、<strong><code><a href="string.constants.php#constant.lc-ctype">LC_CTYPE</a></code></strong> が <code class="literal">en_US.UTF-8</code> の場合、
    1 バイトエンコーディングでエンコードされたデータが
    間違って処理されるかもしれません。
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.str-getcsv-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">separator</code></dt>
     <dd>
      <p class="para">
       <code class="parameter">separator</code> パラメータで、フィールドのデリミタを設定します。
       シングルバイト文字 1 文字のみでなければなりません。
      </p>
     </dd>
    
    
     <dt><code class="parameter">enclosure</code></dt>
     <dd>
      <p class="para">
       オプションの <code class="parameter">enclosure</code> パラメータで、フィールド囲いこみ文字を設定します。
       シングルバイト文字 1 文字のみでなければなりません。
      </p>
     </dd>
    
    
     <dt><code class="parameter">escape</code></dt>
     <dd>
      <p class="para">
       オプションの <code class="parameter">escape</code> パラメータで、エスケープ文字を設定します。
       シングルバイト文字 1 文字のみ、または空文字列でなければなりません。
       空文字列(<code class="literal">&quot;&quot;</code>) を指定すると、(RFC 4180 に準拠していない) 独自仕様のエスケープ機構が無効になります。
      </p>
      <div class="warning"><strong class="warning">警告</strong>
       <p class="simpara">
        入力ストリームにおいて、<code class="parameter">enclosure</code> 文字は
        クォートされた文字列の中で2回連続させることで常にエスケープでき、
        パース結果では単一の <code class="parameter">enclosure</code> 文字になります。
        <code class="parameter">escape</code> 文字の動作は異なります:
        入力中に <code class="parameter">escape</code> と
        <code class="parameter">enclosure</code> 文字の並びが出現した場合、
        パース結果には両方の文字がそのまま残ります。
        よって、デフォルトのパラメータの場合、
        <code class="literal">&quot;a&quot;&quot;b&quot;,&quot;c\&quot;d&quot;</code> のような CSV の行は
        それぞれ <code class="literal">a&quot;b</code> と <code class="literal">c\&quot;d</code>
        にパースされます。
       </p>
      </div>
      <div class="warning"><strong class="warning">警告</strong>
       <p class="simpara">
        PHP 8.4.0 以降では、<code class="parameter">escape</code>
        のデフォルト値に依存することは非推奨となりました。
        位置指定の引数か、<a href="functions.arguments.php#functions.named-arguments" class="link">名前付き引数</a>
        を使用して明示的に指定する必要があります。
       </p>
      </div>
     </dd>
    
   </dl>
  </p>
  <div class="warning"><strong class="warning">警告</strong><p class="simpara">
<code class="parameter">escape</code>
が空の文字列(<code class="literal">&quot;&quot;</code>)以外に設定されているとき、
<a href="https://datatracker.ietf.org/doc/html/rfc4180" class="link external">&raquo;&nbsp;RFC 4180</a>
に準拠しない CSV が生成されたり、PHP の CSV
関数を介してラウンドトリップ(往復変換)でデータが壊れる可能性があります。
<code class="parameter">escape</code>のデフォルト値は<code class="literal">&quot;\\&quot;</code>
なので、明示的に空の文字列を指定することを推奨します。デフォルト値は、PHP 9.0
以降の将来のバージョンで変更予定です。
</p></div>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.str-getcsv-returnvalues">
  <h3 class="title">戻り値</h3>
  <p class="para">
   読み込んだフィールドの内容を配列で返します。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.str-getcsv-errors">
  <h3 class="title">エラー / 例外</h3>
  <p class="simpara">
   <code class="parameter">separator</code> または <code class="parameter">enclosure</code> が
   1 バイト長ではない場合、<span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> をスローします。
  </p>
  <p class="simpara">
   <code class="parameter">escape</code> が 1 バイト長、または空文字列ではない場合、
   <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> をスローします。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.str-getcsv-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>8.4.0</td>
       <td>
        <code class="parameter">escape</code> のデフォルト値に依存することは、
        非推奨になりました。
       </td>
      </tr>

      <tr>
       <td>8.4.0</td>
       <td>
        <code class="parameter">separator</code>、<code class="parameter">enclosure</code>、
        または <code class="parameter">escape</code> が不正な場合、
        <span class="exceptionname"><a href="class.valueerror.php" class="exceptionname">ValueError</a></span> をスローするようになりました。
        これは、<span class="function"><a href="function.fgetcsv.php" class="function">fgetcsv()</a></span> と <span class="function"><a href="function.fputcsv.php" class="function">fputcsv()</a></span>
        の動作に倣ったものとなります。
       </td>
      </tr>

      <tr>
       <td>8.3.0</td>
       <td>
        最後のフィールドが、
        閉じられていないフィールド囲いこみ文字だけの場合、
        null バイト 1 つの文字列ではなく、空文字列を返すようになりました。
       </td>
      </tr>

      <tr>
       <td>7.4.0</td>
       <td>
        <code class="parameter">escape</code> 引数は、
        空文字列を、(RFC 4180 に準拠していない) 独自仕様のエスケープ機構を無効にするシグナルとして解釈するようになりました。
        これより前のバージョンでは、空文字列はデフォルト値のように扱われていました。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.str-getcsv-examples">
  <h3 class="title">例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>例1 <span class="function"><strong>str_getcsv()</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 /><br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">'PHP,Java,Python,Kotlin,Swift'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">str_getcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #0000BB">escape</span><span style="color: #007700">: </span><span style="color: #DD0000">'\\'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は以下となります。</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">array(5) {
  [0]=&gt;
  string(3) &quot;PHP&quot;
  [1]=&gt;
  string(4) &quot;Java&quot;
  [2]=&gt;
  string(6) &quot;Python&quot;
  [3]=&gt;
  string(6) &quot;Kotlin&quot;
  [4]=&gt;
  string(5) &quot;Swift&quot;
}</pre>
</div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-2">
    <p><strong>例2 <span class="function"><strong>str_getcsv()</strong></span> に空文字列を指定した例</strong></p>
    <div class="caution"><strong class="caution">警告</strong>
     <p class="simpara">
      この関数に空文字列を指定すると、
      空の配列ではなく 値 <code class="code">[null]</code> を返します。
     </p>
    </div>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /><br />$string </span><span style="color: #007700">= </span><span style="color: #DD0000">''</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$data </span><span style="color: #007700">= </span><span style="color: #0000BB">str_getcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">, </span><span style="color: #0000BB">escape</span><span style="color: #007700">: </span><span style="color: #DD0000">'\\'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>上の例の出力は以下となります。</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">array(1) {
  [0]=&gt;
  NULL
}</pre>
</div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.str-getcsv-seealso">
  <h3 class="title">参考</h3>
  <ul class="simplelist">
   <li><span class="function"><a href="function.fputcsv.php" class="function" rel="rdfs-seeAlso">fputcsv()</a> - 行を CSV 形式にフォーマットし、ファイルポインタに書き込む</span></li>
   <li><span class="function"><a href="function.fgetcsv.php" class="function" rel="rdfs-seeAlso">fgetcsv()</a> - ファイルポインタから行を取得し、CSVフィールドを処理する</span></li>
   <li><span class="methodname"><a href="splfileobject.fgetcsv.php" class="methodname" rel="rdfs-seeAlso">SplFileObject::fgetcsv()</a> - ファイルから行を取り出し CSV フィールドとして処理する</span></li>
   <li><span class="methodname"><a href="splfileobject.fputcsv.php" class="methodname" rel="rdfs-seeAlso">SplFileObject::fputcsv()</a> - フィールドの配列を CSV の行として書き出す</span></li>
   <li><span class="methodname"><a href="splfileobject.setcsvcontrol.php" class="methodname" rel="rdfs-seeAlso">SplFileObject::setCsvControl()</a> - CSV の区切り文字、囲み文字、エスケープ文字をセットする</span></li>
   <li><span class="methodname"><a href="splfileobject.getcsvcontrol.php" class="methodname" rel="rdfs-seeAlso">SplFileObject::getCsvControl()</a> - CSV の区切り、囲み文字、エスケープ文字を取得する</span></li>
  </ul>
 </div>

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