[Gllug] OT: Regular Expression Help Needed Please

Mike Leigh MikeL at hornbill.com
Thu Oct 7 13:59:50 UTC 2004


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

-----Original Message-----
From: Garry Heaton [mailto:garry at heaton6.freeserve.co.uk]
Sent: 07 October 2004 14:55
To: Greater London Linux Users Group
Subject: Re: [Gllug] OT: Regular Expression Help Needed Please


Mike Leigh wrote:

> Hi,
> 
> Thanks for the info, however I have to use a regex within a php script as
I
> there is a whole host of code written around it and I just found the bug
> this morning.  My regex skills also being soemwhat lacking I have no idea
> how to accomplish this.
> 
> Thanks

Why is fget_csv() not giving you what you want?


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




More information about the GLLUG mailing list