This question already has answers here:
Replace Comma(,) with Dot(.) RegEx php
(5 answers)
Closed 9 years ago.
I have string
10.2, 200.3, 33.00
and I want it to be replaced as
10,2, 200,3, 33,00
I tried
preg_replace("/[.]$/","/\d[,]$/",$input);
but it is not replacing!
I can't use str_replace because it's task in university
Do not use regular expresion when dumb str_replace() suffices:
$str = str_replace('.', ',', $str)
See docs: http://php.net/str_replace
preg_replace('/\./', ',', $input);
This would replace all . dots with ,.
preg_replace('/(\d+).(\d+)/', '$1,$2', $input);
This is more specific to your need. $1 replaces first digit as in parenthesis; $2 replaces second.
You can try this
preg_replace('/[^0-9\s]/', ',', $input)
but it is better if you use
str_replace('.', ',', $input)
as Marcin answered.
Related
This question already has answers here:
How to replace all occurrences of two substrings with str_replace()?
(3 answers)
Closed 5 years ago.
Say I want to erase any quote from this string, and then replace spaces with '&', in php. I could easily do with 2 consecutive preg_replace or the like, but how to do it in only 1 passage?
" columns="4" link="file" ids="280,281,282,283,284,285,286,287,288""
to:
columns=4&link=file&ids=280,281,282,283,284,285,286,287,288
You don't need regex for that. str_replace() should work fine:
$string = 'columns="4" link="file" ids="280,281,282,283,284,285,286,287,288"';
$replaced = str_replace([' ', '"'], ['&', ''], $string);
Demo: https://3v4l.org/gHHMp
This question already has answers here:
Remove all special characters from a string [duplicate]
(3 answers)
Closed 5 years ago.
I want to replace space with "|" and i want only numbers and alphabets and star(*) in the sting
And i have tried str_replace() funtion but it replace the space with | , but i want to validate
echo $text= str_replace(' ','|', "This is some $123 Money");
output
This|is|some|$123|Money
what i expect is
This|is|some|123|Money
I don't want any other special characters in my output
Any Suggestions
Thanks in Advance
You can use preg_replace to remove all special characters from your string.
Example
echo $text= str_replace(' ','|', preg_replace('/[^A-Za-z0-9 ]/', '',"This is some $123 Money"));
Output
This|is|some|123|Money
Here is the working demo for you: https://3v4l.org/M456J
<?php
$s = "This is some $123 Money";
$result = preg_replace("/[^a-zA-Z0-9]+/", "|", $s);
echo $result;
Output:
This|is|some|123|Money
This question already has an answer here:
Cannot work out a php str_replace() to remove comma [closed]
(1 answer)
Closed 2 years ago.
I have the following string:
Input:
$str = "I want to remove only comma from this string, how ?";
I want to remove commas from $str, I'm new in programming and I don't understand how regex works.
use str_replace.
Example
$str = "I want to remove only comma from this string, how ?";
$str = str_replace(",", "", $str);
Explaination
As you can see there is 3 arguments we pass in str_replace
"," => this one is what you want to replace
"" => this one is value that will replace first argument value. we pass blank so it will replace comma to blank
this one is string where you want to replace.
Regex: (?<!\d)\,(?!\d)
(\,|\.) for matching exact either , or .
(?!\d) should not contains digits ahead.
(?<!\d) should not contains digit behind.
PHP code:
<?php
$str = "I want to remove only comma from this string, how. ? Here comma and dot 55,44,100.6 shouldn't be removed";
echo preg_replace("/(?<!\d)(\,|\.)(?!\d)/", "", $str);
Output:
I want to remove only comma from this string how ? Here comma 55,44,100 shouldn't be removed
This question already has answers here:
Remove Text Between Parentheses PHP
(7 answers)
Closed 9 years ago.
Say you have a string like this:
This is a string (with parenthesis stuff)
How would you change that to
This is a string
?
Replace it:
preg_replace('/\(.*?\)/', '', $str);
Use a regex.
Replace \([^)]*\) with the empty string.
Note: If there's more than one pair of parenthesis this will replace the innermost one.
try this
$string = "This is a string (with parenthesis stuff)";
echo preg_replace("/\([^)]+\)/","",$string); // 'ABC '
or you can do this also
$str = "This is a string (with parenthesis stuff)";
$str = trim(preg_replace('/\s*\([^)]*\)/', '', $str));
This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 12 months ago.
How to trim multiple characters at begin and end of string.
string should be something like {Hello {W}orld}.
i want to trim both { and } at begin and end.
don't want to use multiple trim function.
Use the optional second argument to trim which allows you to specify the list of characters to trim:
<?php
$str = "{Hello {W}orld}";
$str = trim($str, "{}");
echo "Trimmed: $str";
Output:
Trimmed: Hello {W}orld
Is there always a character at the beginning and at the end, that you want to remove? If so you could just use the following:
<?php
$string = '{Hello {W}orld}';
echo substr( $string, 1, strlen( $string )-2 );
?>
See: http://codepad.org/IDbG6Km2