8p_tdq
Last Updated: February 25, 2016
·
1.139K
· hobnob
Dave

Getting CSV data from a string

If you have a small CSV file stored in a string, and don't want to implement your own CSV parser or read the data into and out of a stream using fgetcsv, str_getcsv is the perfect function for the job.

For example:

$rows    = explode("\r\n", $csv);
$headers = str_getcsv(trim(array_shift($rows)));

foreach( $rows as $row )
{
    $row = str_getcsv(trim($row));
    //Processing here
}

You can find the docs on this useful function here:
http://php.net/manual/function.str-getcsv.php

Say Thanks
Respond