<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Team Tutorials &#187; comma separated value. fgetcsv</title>
	<atom:link href="http://teamtutorials.com/tag/comma-separated-value-fgetcsv/feed" rel="self" type="application/rss+xml" />
	<link>http://teamtutorials.com</link>
	<description></description>
	<lastBuildDate>Tue, 27 Dec 2011 18:58:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>How to Parse a CSV File Using PHP</title>
		<link>http://teamtutorials.com/web-development-tutorials/how-to-parse-a-csv-file-using-php?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-parse-a-csv-file-using-php</link>
		<comments>http://teamtutorials.com/web-development-tutorials/how-to-parse-a-csv-file-using-php#comments</comments>
		<pubDate>Thu, 18 Jun 2009 22:07:06 +0000</pubDate>
		<dc:creator>John Ward</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Development Tutorials]]></category>
		<category><![CDATA[comma separated value. fgetcsv]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[fopen]]></category>
		<category><![CDATA[parse]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://teamtutorials.com/?p=1850</guid>
		<description><![CDATA[In this tutorial you will learn a simple way to parse CSV files using PHP and output the text of the fields you need. This tutorial is not going to go into what you can do with the data once you get it from the CSV as the possibilities are endless. We will simply show you how to get the data into an array in PHP.]]></description>
			<content:encoded><![CDATA[<p>In this tutorial you will learn a simple way to parse CSV files using PHP and output the text of the fields you need. This tutorial is not going to go into what you can do with the data once you get it from the CSV as the possibilities are endless. We will simply show you how to get the data into an array in PHP.</p>
<p><a href="http://teamtutorials.com/wp-content/uploads/2009/06/test.csv"><br />
First you can download our test.csv file here. </a></p>
<p>Now we are going to open the file for reading using the fopen() function in PHP. Will will specify the location of the file and also the mode in which we want to open it. In this case we are using the &#8220;R&#8221; mode which stands for read. Other possible uses of this function can be found on the <a href="http://us2.php.net/manual/en/function.fopen.php">manual page for fopen()</a>.</p>
<pre class="brush: php; title: Code Snippet:; notranslate">
&lt;?php

$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);

?&gt;
</pre>
<p>Next we are going to create a while loop. We will use this loop to parse each line of the CSV.</p>
<pre class="brush: php; title: Code Snippet:; notranslate">
&lt;?php
$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);

while () {

}
?&gt;
</pre>
<p>Next we are going to use the <a href="http://us3.php.net/manual/en/function.fgetcsv.php">fgetcsv() function </a>to &#8220;split&#8221; each line of our CSV file into an array called $data. When there are no lines left the condition will be false and will end the loop. In the fgetcsv() function we will pass the file handle, length (optional) and the separating character (which is a comma in this case.<br />
test</p>
<pre class="brush: php; title: Code Snippet:; notranslate">
&lt;?php

$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);

while (($data = fgetcsv($handle, 5000, &quot;,&quot;)) !== FALSE) {

}
?&gt;
</pre>
<p>*note: If you data fields are encapsulated using a character such as &#8211; (for ease of example) you can specify the enclosure filed like so: fgetcsv($handle, 5000, &#8220;,&#8221;,&#8221;-&#8221;). By default the enclosure character is a double quote, but the function will also handle no enclosure character.</p>
<p>Next all we have to do is echo the fields that we want to see. We will use the field number in the array to do this. I will echo the second value in the CSV.</p>
<pre class="brush: php; title: Code Snippet:; notranslate">
&lt;?php

$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);
$row = 1;
while (($data = fgetcsv($handle, 5000, &quot;,&quot;)) !== FALSE) {
	echo $data[2];
	echo &quot;&lt;/br&gt;&quot;;
}
?&gt;
</pre>
<p>Another note. If you do not know which value the field you are looking for will be in the array just print the array like this:</p>
<pre class="brush: php; title: Code Snippet:; notranslate">
&lt;?php

$handle = fopen(&quot;test.csv&quot;, &quot;r&quot;);

while (($data = fgetcsv($handle, 5000, &quot;,&quot;)) !== FALSE) {
	echo &quot;&lt;pre&gt;&quot;;
	print_r($data);
	echo &quot;&lt;pre&gt;&quot;;
}
?&gt;
</pre>
<p>As you can see it is pretty simple to parse csv using this method. You may have issue with memory if you try to open large files using fopen. in that case you will want to read the file line-by-line, but that is another tutorial.</p>
<h2  class="related_post_title">Related Posts</h2><ul class="related_post"><li><a href="http://teamtutorials.com/web-development-tutorials/access-a-mysql-database-using-pdo" title="Access a MySQL Database Using PDO">Access a MySQL Database Using PDO</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/using-jquery-to-reorder-a-list-and-update-a-database" title="Using jQuery to Reorder a List and Update a Database">Using jQuery to Reorder a List and Update a Database</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/sending-e-mail-to-validate-user-sign-up" title="Sending E-Mail to validate User Sign-up">Sending E-Mail to validate User Sign-up</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/how-to-return-mysql-results-to-a-table" title="How to Return MySQL Results to a Table">How to Return MySQL Results to a Table</a></li><li><a href="http://teamtutorials.com/web-development-tutorials/editing-mysql-data-using-php" title="Editing MySQL Data Using PHP">Editing MySQL Data Using PHP</a></li></ul>]]></content:encoded>
			<wfw:commentRss>http://teamtutorials.com/web-development-tutorials/how-to-parse-a-csv-file-using-php/feed</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>

