Reputation: 55
Below is my string and I want to remove $ only from integers and float values and don't want to remove $ from string like "Mastsdsdosmsy $4.50 AsI" Please can any one help me.
$string = ' "2016-02-19","Videomssdsdsize",
"Mastsdsdosmsy $4.50 AsI","","Masdsdtomy
$2.00 In-App","27753492","171352","155928",
"109608","0.62","91.00","70.29","$2.25",
"$246.62","$219.22","$27.40","11.11","32",
"0.03","78937","72.02"';
I am already using below regular expression to remove $ but it's removeing from string as well but I don't want to remove $ from string as I mentioned above string Thanks
$result=str_replace('$','' , $string);
Upvotes: 2
Views: 211
Reputation: 26861
try with:
$result=preg_replace('/"\$(\d)/','"$1' , $string);
I haven't tested this solution. The approach is to use a regular expression to match every $
character followed by a number and replace it wit an empty string.
Edit:
I've edited the regexp to match and capture the numbers after the $
sign and to replace the whole match just with the matched number
Edit 2:
I've edited the regex to do the replace only when the $
sign is after a "
Upvotes: 1
Reputation: 626950
You can use the following regex replacement:
'~"\$(\d+(?:\.\d+)?")~'
And replace with "$1
. See regex demo
This regex will match:
"\$
- a "
followed with a literal $
symbol(\d+(?:\.\d+)?")
- Group 1 matching:
\d+
- one or more digits(?:\.\d+)?
- an optional group (one or zero occurrences) matching a literal dot followed by one or more digits"
- a double quoteUpvotes: 0
Reputation: 416
You need to enclose your search string in delimiters first so
preg_replace('/ /',...
or
preg_replace('! !',...
so
$result=preg_replace('/"\$/','"', $string);
What this is doing:
Search for a speach mark "
then a dollar sign \$ (escaped or it would be searching for an end of line)
And replacing that with "
Upvotes: 0