Parsing CSV Strings : CSV File « File Directory « PHP






Parsing CSV Strings

 
<?php
function parse_csv($string, $delim = ',', $quote='"') {
    $new = str_replace("{$quote}{$quote}", $quote, $string);

    $matches = array();
    preg_match_all("/\s*({$quote}?)(.*?)\\1\s*(?:{$delim}|$)/",
        $new, $matches);

    array_pop($matches[2]);
    return $matches[2];
}

$str = 'c "s""v""","s", "f\ny" ';

$values = parse_csv($str);

echo '<pre>';
print_r($values);
echo '</pre>';
?>
  
  








Related examples in the same category

1.Generating comma-separated data
2.Making a CSV-formatted string
3.Read csv file
4.Reading CSV data from a file
5.Tell the web client to view the CSV file in a seprate program
6.fgetcsv.php
7.Printing comma-separated data
8.Putting comma-separated data into a string