{"id":1850,"date":"2009-06-18T18:07:06","date_gmt":"2009-06-18T22:07:06","guid":{"rendered":"http:\/\/teamtutorials.com\/?p=1850"},"modified":"2013-02-15T13:20:12","modified_gmt":"2013-02-15T18:20:12","slug":"how-to-parse-a-csv-file-using-php","status":"publish","type":"post","link":"https:\/\/teamtutorials.com\/web-development-tutorials\/how-to-parse-a-csv-file-using-php","title":{"rendered":"How to Parse a CSV File Using PHP"},"content":{"rendered":"
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>\n
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()<\/a>.<\/p>\n Next we are going to create a while loop. We will use this loop to parse each line of the CSV.<\/p>\n
\nFirst you can download our test.csv file here. <\/a><\/p>\n\r\n<?php\r\n\r\n$handle = fopen("test.csv", "r");\r\n\r\n?>\r\n<\/pre>\n
\r\n<?php\r\n$handle = fopen("test.csv", "r");\r\n\r\nwhile () {\r\n\r\n\t\r\n}\r\n?>\r\n<\/pre>\n