Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 3056

php-manual-en-5.5.7-1.mga4.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)</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"> = &#039;,&#039;</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"> = &#039;\\&#039;</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>
 </div>


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

    <dt>

     <span class="term"><em><code class="parameter">handle</code></em></span>
     <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>

    <dt>

     <span class="term"><em><code class="parameter">length</code></em></span>
     <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). It became
       optional in PHP 5. Omitting this parameter (or setting it to 0 in PHP
       5.0.4 and later) the maximum line length is not limited, which is
       slightly slower.
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">delimiter</code></em></span>
     <dd>

      <p class="para">
       Set the field delimiter (one character only).
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">enclosure</code></em></span>
     <dd>

      <p class="para">
       Set the field enclosure character (one character only).
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">escape</code></em></span>
     <dd>

      <p class="para">
       Set the escape character (one character only). Defaults as a backslash.
      </p>
     </dd>

    </dt>

   </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
   <em><code class="parameter">handle</code></em> 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 <em><code class="parameter">escape</code></em> parameter was added
       </td>
      </tr>

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

      <tr>
       <td>4.3.0</td>
       <td>
        The <em><code class="parameter">enclosure</code></em> parameter was added
       </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-2361">
    <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 notes" id="refsect1-function.fgetcsv-notes">
  <h3 class="title">Notes</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    Locale setting is taken into account by this function. If
    <var class="varname"><var class="varname">LANG</var></var> is e.g. <em>en_US.UTF-8</em>, files in
    one-byte encoding are read wrong by this function.
   </p>
  </p></blockquote>
 </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 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>