Splitting CSV, but not on comma only (PHP)?

I want to split a CSV string. The string looks like that:

"val1","val2","val3, some other stuff", "val4"

As you see, there can be a comma inside the values (see val3), so using explode on the comma wont work.

I also tried:

preg_split('/","/', $myLine)

This does not work though, how would I do that otherwise?

Thanks for your help :)

Answers


You can use the built in fgetcsv if your data is in a file,

or str_getcsv if your data is a string


$inputfiledelimiter="~";
if (($handle = fopen($target_path, "r")) !== FALSE) 
{       
    while (($data = fgetcsv($handle, 0, $inputfiledelimiter)) !== FALSE) 
    {

    }   
}

For CSV files in PHP function: fgetcsv

In 3rd param you can set delimetr, by default - "," (comma)

Function reads every lines and returns array with elements for current line

Example:

$f = fopen('file.csv', 'r+');
while (($data = fgetcsv($f, 1000, ",")) !== FALSE) {
    /* $data is array with elements for current line */
}
fclose($f);

Need Your Help

Kendo UI Chart - Click Events

javascript jquery asp.net html5 kendo-ui

Does anyone know how to create an on-click event for the Kendo-UI data viz bar graphs?

add additional button on product page for woocommerce

wordpress woocommerce product woothemes variations

I have spent the majority of the day trying to figure out how to adjust the single product page using woocommerce.