CSV Parser for PHP

This is a snippet of code that I had posted on another of my websites and in doing some cleanup it really fits better here.

I spent last evening working on a Comma Separated Values (CSV) list of titles for a project I'm working on. Below is a snippet of code I worked with. The first method from the PHP Cookbook suggests using fgetcsv(). That woks pretty well but the native function in PHP doesn't handle values (fields) that contain separators (commas) very well and doesn't have the concept of multiple values being delimited by a different delimiter within a field. So I whipped up the parser that comes second in the example below.

#000000">#0000BB"><?php
$fp
#007700">= #0000BB">fopen#007700">(#DD0000">'filename'#007700">,#DD0000">'r'#007700">) or die(#DD0000">"can't open file"#007700">);
print
#DD0000">"<table border="#0000BB">1#DD0000">">\n"#007700">;
while(
#0000BB">$csv_line #007700">= #0000BB">fgetcsv#007700">(#0000BB">$fp#007700">,#0000BB">1024#007700">, #DD0000">","#007700">, #DD0000">""")) {
    print "
#007700"><#0000BB">tr#007700">>#DD0000">";
    for (
#0000BB">$i#DD0000"> = 0, #0000BB">$j#DD0000"> = count(#0000BB">$csv_line#DD0000">); #0000BB">$i#DD0000"> < #0000BB">$j#DD0000">; #0000BB">$i#DD0000">++) {
        print "
#007700"><#0000BB">td#007700">>#DD0000">". str_replace ("#007700">;#DD0000">""#007700">, #DD0000">"<br />"#007700">, #0000BB">$csv_line#007700">[#0000BB">$i#007700">]).#DD0000">"</td>"#007700">;
    }
    print
#DD0000">"</tr>\n"#007700">;
}
print
#DD0000">"</table>\n"#007700">;
#0000BB">fclose#007700">(#0000BB">$fp#007700">) or die(#DD0000">"can't close file"#007700">);

#0000BB">$bas #007700">= #0000BB">file_get_contents #007700">(#DD0000">"filename"#007700">);
print
#DD0000">"<table border="#0000BB">1#DD0000">">\n"#007700">;
#0000BB">$lines #007700">= #0000BB">explode#007700">(#DD0000">"\n"#007700">, #0000BB">$bas#007700">);
foreach (
#0000BB">$lines #007700">as #0000BB">$line#007700">) {
    print
#DD0000">'<tr>'#007700">;
   
#0000BB">$line #007700">= #0000BB">ltrim#007700">(#0000BB">$line#007700">, #DD0000">""");
   
#0000BB">$line#DD0000"> = rtrim(#0000BB">$line#DD0000">, """#007700">);
   
#0000BB">$line #007700">= #0000BB">rtrim#007700">(#0000BB">$line#007700">);
   
#0000BB">$out #007700">= #0000BB">str_replace#007700">(#DD0000">""#007700">;#DD0000">""#007700">, #DD0000">"<br />"#007700">, #0000BB">$line#007700">);
   
#0000BB">$out #007700">= #0000BB">str_replace#007700">(#DD0000">""#007700">,#DD0000">""#007700">, #DD0000">"</td><td>"#007700">, #0000BB">$out#007700">);
    print
#DD0000">'<td>' #007700">. #0000BB">$out #007700">. #DD0000">'</td>'#007700">;
        print
#DD0000">"</tr>\n"#007700">;

};

print

#DD0000">"</table>\n"#007700">;
#0000BB">?>

Category: 

1 Comment

Line breaks within a property

First off, great parser. However, I am having an issue with a particular CSV file that contains line breaks within property values.

My specific issue is that some of my Street field values have line breaks which are being split by explode and screwing up the entire result.

Do you know of a way to only explode at line breaks if the line break is not between quotes?

Thanks!