Check out our newest creation Tutorial Grad. The automated tutorial directory.

Subscribe to our rss feed

pc scan tool

How to Parse a CSV File Using PHP

Posted in PHP Tutorials, Web Development Tutorials by John Ward on the June 18th, 2009

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.


First you can download our test.csv file here.

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 “R” mode which stands for read. Other possible uses of this function can be found on the manual page for fopen().

<?php

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

?>

Next we are going to create a while loop. We will use this loop to parse each line of the CSV.

<?php
$handle = fopen("test.csv", "r");

while () {

}
?>

Next we are going to use the fgetcsv() function to “split” 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.
test

<?php

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

while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {

}
?>

*note: If you data fields are encapsulated using a character such as – (for ease of example) you can specify the enclosure filed like so: fgetcsv($handle, 5000, “,”,”-”). By default the enclosure character is a double quote, but the function will also handle no enclosure character.

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.

<?php

$handle = fopen("test.csv", "r");
$row = 1;
while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
	echo $data[2];
	echo "</br>";
}
?>

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:

<?php

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

while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
	echo "<pre>";
	print_r($data);
	echo "<pre>";
}
?>

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.

Popularity: unranked [?]

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Reddit

Related Posts

Tutorial Grad - Recent Tutorials

10 Responses to 'How to Parse a CSV File Using PHP'

Subscribe to comments with RSS or TrackBack to 'How to Parse a CSV File Using PHP'.

  1. Swiss Cheese said,

    on June 22nd, 2009 at 2:15 pm

    I’m trying to parse a CSV file with ruby and have not found any good tutorials online. Also, I’ve looked at the API documentation and its still difficult/tedious to follow.

  2. serengeti said,

    on July 10th, 2009 at 1:29 am

    If I use your tutorial, special characters in the csv like ‘, %, etc are not read correctly and when displayed look like ’

  3. Buy PSP Go said,

    on July 19th, 2009 at 2:00 pm

    Thanks for the great tutorial. I still have a way to go before I get my CSV to Wordpress Post plugin properly working but this is a much better solution than the one I currently have in place.

  4. RandiR said,

    on July 27th, 2009 at 1:25 pm

    Very good tutorial. I use CSVs a lot for stock market data. If you need to parse stock market data, there is a good sample script posted at http://www.biterscripting.com/SS_CSV.html . There are also some good tutorials on scripting at that site.

    Randi

  5. John Ward said,

    on August 17th, 2009 at 12:58 pm

    @serengeti
    That’s not a problem with the tutorial. Character encoding is a whole different topic. You could try to use utf8_decode(). http://us3.php.net/manual/en/function.utf8-decode.php

  6. generic said,

    on August 26th, 2009 at 7:36 pm

    Many Thanks for the amazing tutorial. I still have a way to go before I get my CSV to Wordpress Post plugin properly working but this is a much better solution than the one I currently have in place.

  7. football said,

    on September 4th, 2009 at 5:11 am

    it is really nice post with a good info.keep it up……


  8. on September 5th, 2009 at 9:47 pm

    i am a beginner of php, i have tried to find the solution for many times. but the official manual is hard to follow. i have test your method, that is ok….but i don’t understand very well, so hoping your next post…

  9. generic said,

    on September 6th, 2009 at 12:40 pm

    Many Thanks for this great tutorial.


  10. on November 3rd, 2009 at 5:48 am

    im writing a script to download a file using php and curl. but downloading is just too slow, i want to open multiple connections to the server and download the file (hence accelerate the download). how can i do this ?

Leave a Comment