<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Adding Understanding</title>
  <subtitle>A place for those who add understanding to the world.</subtitle>
  <link rel="alternate" type="text/html" href="http://addingunderstanding.com/2008/01/csv-parser-php"/>
  <link rel="self" type="application/atom+xml" href="http://addingunderstanding.com/node/1481/atom/feed"/>
  <id>http://addingunderstanding.com/node/1481/atom/feed</id>
  <updated>2008-01-21T17:44:22-07:00</updated>
  <entry>
    <title>CSV Parser for PHP</title>
    <link rel="alternate" type="text/html" href="http://addingunderstanding.com/2008/01/csv-parser-php" />
    <id>http://addingunderstanding.com/2008/01/csv-parser-php</id>
    <published>2008-01-21T17:44:22-07:00</published>
    <updated>2008-01-21T17:44:22-07:00</updated>
    <author>
      <name>joshb</name>
    </author>
    <category term="Code" />
    <category term="PHP" />
    <summary type="html"><![CDATA[<blockquote><p>This is a snippet of code that I had posted on another of my websites and in doing some cleanup it really fits better here. </p>
</blockquote>
<p>I spent last evening working on a Comma Separated Values (CSV) list of titles for a project I'm working on. Below is a snippet of code I worked with. The first method from the <a href="http://www.amazon.com/exec/obidos/redirect?link_code=as2&amp;path=ASIN/1565926811&amp;tag=addingunderst-20&amp;camp=1789&amp;creative=9325">PHP Cookbook</a>  suggests using <a href="http://www.php.net/fgetcsv">fgetcsv()</a>. That woks pretty well but the native function in PHP doesn't handle values (fields) that contain separators (commas) very well and doesn't have the concept of multiple values being delimited by a different delimiter within a field. So I whipped up the parser that comes second in the example below.</p>


    ]]></summary>
    <content type="html"><![CDATA[<blockquote><p>This is a snippet of code that I had posted on another of my websites and in doing some cleanup it really fits better here. </p></blockquote>
<p>I spent last evening working on a Comma Separated Values (CSV) list of titles for a project I'm working on. Below is a snippet of code I worked with. The first method from the <a href="http://www.amazon.com/exec/obidos/redirect?link_code=as2&amp;path=ASIN/1565926811&amp;tag=addingunderst-20&amp;camp=1789&amp;creative=9325">PHP Cookbook</a>  suggests using <a href="http://www.php.net/fgetcsv">fgetcsv()</a>. That woks pretty well but the native function in PHP doesn't handle values (fields) that contain separators (commas) very well and doesn't have the concept of multiple values being delimited by a different delimiter within a field. So I whipped up the parser that comes second in the example below.</p>
<p>&lt;!--break--><br />
<div class="codeblock"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php</span></span></code></div></p>
<p>$fp <span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'filename'</span><span style="color: #007700">,</span><span style="color: #DD0000">'r'</span><span style="color: #007700">) or die(</span><span style="color: #DD0000">"can't open file"</span><span style="color: #007700">);<br />print </span><span style="color: #DD0000">"&lt;table border=\"1\"&gt;\n"</span><span style="color: #007700">;<br />while(</span><span style="color: #0000BB">$csv_line </span><span style="color: #007700">= </span><span style="color: #0000BB">fgetcsv</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,</span><span style="color: #0000BB">1024</span><span style="color: #007700">, </span><span style="color: #DD0000">","</span><span style="color: #007700">, </span><span style="color: #DD0000">"\""</span><span style="color: #007700">)) {<br />&nbsp;&nbsp;&nbsp; print </span><span style="color: #DD0000">'&lt;tr&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp; for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">$j </span><span style="color: #007700">= </span><span style="color: #0000BB">count</span><span style="color: #007700">(</span><span style="color: #0000BB">$csv_line</span><span style="color: #007700">); </span><span style="color: #0000BB">$i </span><span style="color: #007700">&lt; </span><span style="color: #0000BB">$j</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print </span><span style="color: #DD0000">'&lt;td&gt;'</span><span style="color: #007700">. </span><span style="color: #0000BB">str_replace </span><span style="color: #007700">(</span><span style="color: #DD0000">";\""</span><span style="color: #007700">, </span><span style="color: #DD0000">"&lt;br /&gt;"</span><span style="color: #007700">, </span><span style="color: #0000BB">$csv_line</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]).</span><span style="color: #DD0000">'&lt;/td&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp; print </span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;<br />}<br />print </span><span style="color: #DD0000">'&lt;/table&gt;\n'</span><span style="color: #007700">;*/<br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">) or die(</span><span style="color: #DD0000">"can't close file"</span><span style="color: #007700">);</span></p>
<p><span style="color: #0000BB">$bas </span><span style="color: #007700">= </span><span style="color: #0000BB">file_get_contents </span><span style="color: #007700">(</span><span style="color: #DD0000">'filename'</span><span style="color: #007700">);<br />print </span><span style="color: #DD0000">"&lt;table border=\"1\"&gt;\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$lines </span><span style="color: #007700">= </span><span style="color: #0000BB">explode</span><span style="color: #007700">(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$bas</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">$lines </span><span style="color: #007700">as </span><span style="color: #0000BB">$line</span><span style="color: #007700">) {<br />&nbsp;&nbsp;&nbsp; print</span><span style="color: #DD0000">'&lt;tr&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">ltrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$line</span><span style="color: #007700">, </span><span style="color: #DD0000">"\""</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$line</span><span style="color: #007700">, </span><span style="color: #DD0000">"\""</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$line </span><span style="color: #007700">= </span><span style="color: #0000BB">rtrim</span><span style="color: #007700">(</span><span style="color: #0000BB">$line</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$out </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"\";\""</span><span style="color: #007700">, </span><span style="color: #DD0000">"&lt;br /&gt;"</span><span style="color: #007700">, </span><span style="color: #0000BB">$line</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp; </span><span style="color: #0000BB">$out </span><span style="color: #007700">= </span><span style="color: #0000BB">str_replace</span><span style="color: #007700">(</span><span style="color: #DD0000">"\",\""</span><span style="color: #007700">, </span><span style="color: #DD0000">"&lt;/td&gt;&lt;td&gt;"</span><span style="color: #007700">, </span><span style="color: #0000BB">$out</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp; print </span><span style="color: #DD0000">'&lt;td&gt;' </span><span style="color: #007700">. </span><span style="color: #0000BB">$out </span><span style="color: #007700">. </span><span style="color: #DD0000">'&lt;/td&gt;'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print </span><span style="color: #DD0000">"&lt;/tr&gt;\n"</span><span style="color: #007700">;</span></p>
<p>};<br />print <span style="color: #DD0000">'&lt;/table&gt;\n'</span><span style="color: #007700">;</span></p>
<p><span style="color: #0000BB">?&gt;</span>


</p>    ]]></content>
  </entry>
</feed>
