Regular expression for CSV files

Posted: August 22nd, 2006 | Author: | Filed under: PHP | 1 Comment »

After many hours of searching, I found this regular expression to work the best on parsing CSV files:

$expr = “/,(?=(?:[^\"]*\”[^\"]*\”)*(?![^\"]*\”))/”;

Example:

$matches = preg_split($expr, ’2006-01-01, “Apples, Oranges, and Pears”, $5.95′);


One Comment on “Regular expression for CSV files”

  1. 1 Braheem said at 4:48 pm on April 23rd, 2008:

    Thanks! That worked perfectly :)


Leave a Reply