[thelist] Ereg

Matt Warden mwarden at gmail.com
Wed May 17 17:08:53 CDT 2006

Hash: SHA1

Chris Price wrote:
> I am uploading a csv file to the server and then exploding it in php to give
> me data in arrays.
> When an entry in the csv file has a comma in it, that entry is surrounded by
> double quotes which helps to differentiate a textual comma from a delimiting
> comma.
> I want to escape the textual commas and delete the quotes before exploding
> the strings into arrays.
> I figured I could find the occurrences of "foo,foo" using ereg and replace
> them with foo,foo but I'm not sure how I should do that or if there is a
> much simpler solution anyway.

Something like (for simplicity i'll assume you're just converting it
commas to \,):

$subject = 'HI, my name is, "Warden, Matt"';
$foo = preg_replace('/"([^",\]*),([^",]*)"/', '"${1}\,${2}"', $subject);

Should give you:

HI, my name is, "Warden\, Matt"

Untested. You may have to escape some of the things in the expression.
If you run the regex again on the resulting content, it shouldn't
re-escape the comma (that's what the slash in the first character class
is for).


- --
Matt Warden
Oxford, OH, USA

This email proudly and graciously contributes to entropy.
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org


More information about the thelist mailing list