Sophie

Sophie

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

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>Format line as CSV and write to file pointer</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.fpassthru.html">fpassthru</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.fputs.html">fputs</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.fputcsv" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fputcsv</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7)</p><p class="refpurpose"><span class="refname">fputcsv</span> &mdash; <span class="dc-title">Format line as CSV and write to file pointer</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fputcsv-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><strong>fputcsv</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$fields</code></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_char</code><span class="initializer"> = &quot;\\&quot;</span></span>
  ]]] )</div>

  <p class="para rdfs-comment">
   <span class="function"><strong>fputcsv()</strong></span> formats a line (passed as a
   <code class="parameter">fields</code> array) as CSV and write it (terminated by a
   newline) to the specified file <code class="parameter">handle</code>. 
  </p>
 </div>


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

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

     <dd>

      <p class="para">The file pointer must be valid, and must point to
a file successfully opened by <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span> or
<span class="function"><a href="function.fsockopen.html" class="function">fsockopen()</a></span> (and not yet closed by
<span class="function"><a href="function.fclose.html" class="function">fclose()</a></span>).</p>
     </dd>

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

     <dd>

      <p class="para">
       An array of <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s.
      </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 (one character only).
      </p>
     </dd>

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

     <dd>

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

    
   </dl>

  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    If an <code class="parameter">enclosure</code> character is contained in a field,
    it will be escaped by doubling it, unless it is immediately preceded by an
    <code class="parameter">escape_char</code>.
   </p>
  </p></blockquote> 
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fputcsv-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the length of the written string  or <strong><code>FALSE</code></strong> on failure.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.fputcsv-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.5.4</td>
       <td>
        The <code class="parameter">escape_char</code> parameter was added
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fputcsv-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-2887">
    <p><strong>Example #1 <span class="function"><strong>fputcsv()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$list&nbsp;</span><span style="color: #007700">=&nbsp;array&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'aaa'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'bbb'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ccc'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'dddd'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'123'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'456'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'789'</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;array(</span><span style="color: #DD0000">'"aaa"'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'"bbb"'</span><span style="color: #007700">)<br />);<br /><br /></span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'file.csv'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">);<br /><br />foreach&nbsp;(</span><span style="color: #0000BB">$list&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$fields</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fputcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$fields</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will write the following to <em>file.csv</em>:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
aaa,bbb,ccc,dddd
123,456,789
&quot;&quot;&quot;aaa&quot;&quot;&quot;,&quot;&quot;&quot;bbb&quot;&quot;&quot;

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


 <div class="refsect1 notes" id="refsect1-function.fputcsv-notes">
  <h3 class="title">Notes</h3>
  <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>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.fputcsv-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.fgetcsv.html" class="function" rel="rdfs-seeAlso">fgetcsv()</a> - Gets line from file pointer and parse for CSV fields</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.fpassthru.html">fpassthru</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.fputs.html">fputs</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>