Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
amir
Forum Contributor
Posts: 287 Joined: Sat Oct 07, 2006 4:28 pm
Post
by amir » Sun Jan 21, 2007 1:49 pm
Hello,
I am getting input from the user through a CSV file and the format of that string is
Code: Select all
15/11/2006,Hello,Yes,2.1,1,"2,050",45,94.5,0,0,0
. . .
. . .
. . .
and so on.
I want to remove comma from each entry of "2,050". It should be "2050".
TIA.
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Sun Jan 21, 2007 2:16 pm
Code: Select all
$source = '15/11/2006,Hello,Yes,2.1,1,"2,050",45,94.5,0,0,0';
echo preg_replace('#"(\d+),(\d+)"#', '$1$2', $source);
Not tested, but should be ok. Enjoy.
Moved to Regex.
amir
Forum Contributor
Posts: 287 Joined: Sat Oct 07, 2006 4:28 pm
Post
by amir » Sun Jan 21, 2007 2:21 pm
Thanks a lot!
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Sun Jan 21, 2007 2:23 pm
Be aware that the code shown will remove the quotation marks unless you use '"$1$2"' instead. Just in case you wanted to keep them.
joboy
Forum Newbie
Posts: 24 Joined: Mon Oct 23, 2006 8:54 pm
Location: Philippines
Post
by joboy » Tue Jan 23, 2007 1:01 am
what about the opposite scenario, in my case i need to retain the commas. let's say the value in a field(of a table) contains similar to this:
"apple, banna, and mango. " (pls. exlude the double quotes).
how will i put this single value to a single cell in excel?..what i have right now is every "fruit" were separately placed in three adjacet cells, obviously because they are comma separated values (CSV).
thanks.
joboy
Forum Newbie
Posts: 24 Joined: Mon Oct 23, 2006 8:54 pm
Location: Philippines
Post
by joboy » Tue Jan 23, 2007 1:53 am
never mind guys, if a variable or field's content contains comma(s)..what's needed is just to enclosed it with double quotes.