Last Updated: February 25, 2016
·
2.472K
· hobnob

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

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Related Tags
#php
#csv
Did you know?

PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.

Security insights from Cside