Saturday, April 13, 2013

Handle csv file using php

4/13/2013

Assume, I got the following content in my csv file:


"data", "data1", "data2", "data3""data", "data1", "data2", "data3""data", "data1", "data2", "data3"






You can use fgetcsv to parse a CSV file without having to worry about parsing it yourself.

example from php.net



<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "

$num fields in line $row:

\n"; $row++; for ($c=0; $c < $num; $c++) { echo $data[$c] . " \n"; } } fclose($handle); } ?>

helpful? Share this

The Editorial Team of 4everTutorials consists of a group of PHP Professionals.

0 comments:

 

© 2014 4everTutorials. All rights resevered.

Back To Top