[Gllug] OT: Regular Expression Help Needed Please

Garry Heaton garry at heaton6.freeserve.co.uk
Thu Oct 7 14:46:11 UTC 2004


Mike Leigh wrote:

> Hi,
> 
> I am not sure why fgetcsv is not giving me the correct results but it seems
> to be ignoring the optional string enclosure.  If I have a line with just
> commas then fgetcsv will work as expected.  If I have a line with " and ,
> then fgetcsv give strange results in the array.  Thats why I wrote the regex
> version.
> 
> Below is the code I wrote to do what fgetcsv does.
> 
> 	$file = file_get_contents($config["csv_file"]);
> 	$old_array = explode("\n", $file);
> 	$data = array();
> 	foreach ($old_array as $line) {
> 		//remove commas from within quotation marks. e.g. from
> [this,",,i,s,, a,",string] to [this,"is a",string]
> 		if (preg_match("/\"[\d\D]*,+[\d\D]*\"/",$line, $matches)) {
> 			$line = str_replace($matches[0], str_replace(",",
> "", $matches[0]), $line);
> 		}
> 		$data[] = $line;
> 	}
> 	for ($i = 0; $i <= count($data) -1; $i++) {
> 		$data[$i] = explode(",", $data[$i]);
> 		
> 	}
> This gives the same output as fgetcsv.  All I need to fix is the regex and
> then start hammering fgetcsv and my replacement with test cases to try and
> see where fgetcsv goes astray.
> 
> Mike

Off the top of my head this Perl would get rid of the double-quoted commas:

$line =~ s/"[,([^,]*)]+"/"$1"/g;

Try massaging it into PHP.

Regards

Garry





-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list