Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to parse a csv file in php when the delimiter is unknown

There are plenty of MS Excel alternatives out there – OpenOffice, LibreOffice, Kingsoft, Google Spreadsheets and many more. The problem is that Csv Files created by different softwares may vary (, or ; to name a few options). If you need to process CSV files exported from different systems and software and handle the delimiter automatically, you can benefit from using SplFileObject::getCsvControl method.

Example usage:

getCsvControl();

$lines = fopen($_FILES["csv"]["tmp_name"], 'r');
if($lines) {
while (($line = fgetcsv($lines, 4096, $delimiter, $enclosure)) !== false) {
//do something
}
}

Reference: http://php.net/manual/en/splfileobject.getcsvcontrol.php



This post first appeared on Vladimir Ivanov's Tech, please read the originial post: here

Share the post

How to parse a csv file in php when the delimiter is unknown

×

Subscribe to Vladimir Ivanov's Tech

Get updates delivered right to your inbox!

Thank you for your subscription

×