How to remove comma in the string of Amount of 2,050

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
amir
Forum Contributor
Posts: 287
Joined: Sat Oct 07, 2006 4:28 pm

How to remove comma in the string of Amount of 2,050

Post by amir »

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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

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 »

Thanks a lot!
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

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.
User avatar
joboy
Forum Newbie
Posts: 24
Joined: Mon Oct 23, 2006 8:54 pm
Location: Philippines

Post by joboy »

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.
User avatar
joboy
Forum Newbie
Posts: 24
Joined: Mon Oct 23, 2006 8:54 pm
Location: Philippines

Post by joboy »

never mind guys, if a variable or field's content contains comma(s)..what's needed is just to enclosed it with double quotes.
Post Reply