Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 3583

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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>Gets line from file pointer and parse for CSV fields</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.fgetc.html">fgetc</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.fgets.html">fgets</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.fgetcsv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fgetcsv</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">fgetcsv</span> &mdash; <span class="dc-title">Gets line from file pointer and parse for CSV fields</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fgetcsv-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>fgetcsv</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$delimiter</code><span class="initializer"> = &quot;,&quot;</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$enclosure</code><span class="initializer"> = &#039;&quot;&#039;</span></span>
   [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$escape</code><span class="initializer"> = &quot;\\&quot;</span></span>
  ]]]] )</div>

  <p class="para rdfs-comment">
   Similar to <span class="function"><a href="function.fgets.html" class="function">fgets()</a></span> except that
   <span class="function"><strong>fgetcsv()</strong></span> parses the line it reads for fields in
   <acronym title="Comma Separated Value">CSV</acronym> format and returns an array containing the fields
   read.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    The locale settings are taken into account by this function. If
    <em>LC_CTYPE</em> is e.g. <em>en_US.UTF-8</em>, files in
    one-byte encodings may be read wrongly by this function.
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fgetcsv-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">handle</code></dt>

     <dd>

      <p class="para">
       A valid file pointer to a file successfully opened by
       <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>, <span class="function"><a href="function.popen.html" class="function">popen()</a></span>, or
       <span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       Must be greater than the longest line (in characters) to be found in
       the CSV file (allowing for trailing line-end characters). Otherwise the
       line is split in chunks of <code class="parameter">length</code> characters,
       unless the split would occur inside an enclosure.
      </p>
      <p class="para">
       Omitting this parameter (or setting it to 0 in PHP
       5.1.0 and later) the maximum line length is not limited, which is
       slightly slower.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">delimiter</code></dt>

     <dd>

      <p class="para">
       The optional <code class="parameter">delimiter</code> parameter sets the field delimiter (one character only).
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">enclosure</code></dt>

     <dd>

      <p class="para">
       The optional <code class="parameter">enclosure</code> parameter sets the field enclosure character (one character only).
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">escape</code></dt>

     <dd>

      <p class="para">
       The optional <code class="parameter">escape</code> parameter sets the escape character (one character only).
      </p>
      <blockquote class="note"><p><strong class="note">Note</strong>: 
       <span class="simpara">
        Usually an <code class="parameter">enclosure</code> character is escaped inside
        a field by doubling it; however, the <code class="parameter">escape</code>
        character can be used as an alternative. So for the default parameter
        values <em>&quot;&quot;</em> and <em>\&quot;</em> have the same
        meaning. Other than allowing to escape the
        <code class="parameter">enclosure</code> character the
        <code class="parameter">escape</code> character has no special meaning; it isn&#039;t
        even meant to escape itself.
       </span>
      </p></blockquote>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fgetcsv-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns an indexed array containing the fields read.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    A blank line in a CSV file will be returned as an array
    comprising a single <span class="type"><a href="language.types.null.html" class="type null">null</a></span> field, and will not be treated
    as an error.
   </p>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara">If PHP is not properly recognizing
the line endings when reading files either on or created by a Macintosh
computer, enabling the
<a href="filesystem.configuration.html#ini.auto-detect-line-endings" class="link">auto_detect_line_endings</a>
run-time configuration option may help resolve the problem.</span></p></blockquote>
  <p class="para">
   <span class="function"><strong>fgetcsv()</strong></span> returns <strong><code>NULL</code></strong> if an invalid
   <code class="parameter">handle</code> is supplied or <strong><code>FALSE</code></strong> on other errors,
   including end of file.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fgetcsv-changelog">
  <h3 class="title">Changelog</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>Version</th>
       <th>Description</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.3.0</td>
       <td>
        The <code class="parameter">escape</code> parameter was added
       </td>
      </tr>

      <tr>
       <td>5.1.0</td>
       <td>
        The <code class="parameter">length</code> is now optional.
        Default is <em>0</em>, meaning no length limit.
        
       </td>
      </tr>

      <tr>
       <td>4.3.5</td>
       <td>
        <span class="function"><strong>fgetcsv()</strong></span> is now binary safe
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fgetcsv-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-2861">
    <p><strong>Example #1 Read and print the entire contents of a CSV file</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$row&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">;<br />if&nbsp;((</span><span style="color: #0000BB">$handle&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"test.csv"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"r"</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;((</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fgetcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1000</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">","</span><span style="color: #007700">))&nbsp;!==&nbsp;</span><span style="color: #0000BB">FALSE</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$num&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$data</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&lt;p&gt;&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #DD0000">&nbsp;fields&nbsp;in&nbsp;line&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #DD0000">:&nbsp;&lt;br&nbsp;/&gt;&lt;/p&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$c</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$c&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$num</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$c</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">[</span><span style="color: #0000BB">$c</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"&lt;br&nbsp;/&gt;\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$handle</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

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


 <div class="refsect1 seealso" id="refsect1-function.fgetcsv-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.str-getcsv.html" class="function" rel="rdfs-seeAlso">str_getcsv()</a> - Parse a CSV string into an array</span></li>
    <li class="member"><span class="function"><a href="function.explode.html" class="function" rel="rdfs-seeAlso">explode()</a> - Split a string by a string</span></li>
    <li class="member"><span class="function"><a href="function.file.html" class="function" rel="rdfs-seeAlso">file()</a> - Reads entire file into an array</span></li>
    <li class="member"><span class="function"><a href="function.pack.html" class="function" rel="rdfs-seeAlso">pack()</a> - Pack data into binary string</span></li>
    <li class="member"><span class="function"><a href="function.fputcsv.html" class="function" rel="rdfs-seeAlso">fputcsv()</a> - Format line as CSV and write to file pointer</span></li>
   </ul>
  </p>
 </div>


</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.fgetc.html">fgetc</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.fgets.html">fgets</a></div>
 <div class="up"><a href="ref.filesystem.html">Filesystem Functions</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>