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
Related
This question already has answers here:
What is the difference between .*? and .* regular expressions?
(3 answers)
Closed 4 years ago.
I have a string:
$str = '{:de}Hallo Welt{:}{:en}Helo world{:}';
now i need to get substring for setted language.
$placeholder = '{:en}';
$lang_content = preg_replace('/(.*)'.preg_quote($placeholder).'(.*)'.preg_quote('{:}').'(.*)/sm', '\2', $str);
in case of value for placeholder {:en} i get Helo world as value for $lang_content, it works fine, but in case {:de} i get Hallo Welt{:}{:en}Helo world.
How can it be fixed? thanx!
You need to make the regex lazy by adding a ? in the second capture.
$str = '{:de}Hallo Welt{:}{:en}Helo world{:}';
$placeholder = '{:de}';
$lang_content = preg_replace('/(.*)'.preg_quote($placeholder).'(.*?)'.preg_quote('{:}').'(.*)/sm', '\2', $str);
echo $lang_content; // Hallo Welt
https://3v4l.org/m6AGF
An optional method would be to use preg_match to get an associative array.
$str = '{:de}Hallo Welt{:}{:en}Helo world{:}';
preg_match_all("/\{:(.*?)}(.*?)\{/", $str, $matches);
$matches = array_combine($matches[1], $matches[2]);
var_dump($matches);
echo $matches["de"]; // Hallo Welt
https://3v4l.org/ELB2B
With the Ungreedy flag (U).
You can update the flags at the end of your regex from /sm to /smU for the result to stop at the next occurrence of {:} in the string and not at the last one.
If the string is to contain several occurrences, maybe you can also add the global (g) flag so the regex doesn't stop after the first one.
You can test it here: https://regex101.com/r/4XSPoz/3
By the way, it seems that your are using preg_replace as a way to find a substring and don't actually need to replace it in the result, maybe you should use preg_match instead.
I am trying to hip of from the end dot or comma and zero's using php
String is €69,00
Code is return preg_replace('/[\.,]{1}0*$/', '', $_price);
Search from right using dollar; find multipl zero's until you find the first comma or dot (from the right). Replace it with empty string.
Original request: show a formatted price that is integer, as a floor value without decimals.
String 1 in €69,00 and out €69
String 2 in €69,80 and out €69,80
String 3 in €69,95 and out €69,95
Somehow this is not working
question: How can I fix the regex to make integer whole values show without the decimals when the string is already formatted (not a number)
Another variation would be to look only for ,00:
(€\d+)(?:[,.]00)
... and replace this with $1, see a demo on regex101.com.
Which in PHP would be:
<?php
$string = <<<DATA
String 1 in €69,00 and out €69
String 2 in €69,80 and out €69,80
String 3 in €69,95 and out €69,95
DATA;
$regex = '~(€\d+)(?:[,.]00)~';
$string = preg_replace($regex, "$1", $string);
echo $string;
?>
try this one
preg_replace('/([€$])(\d+)([.,])(\d+)/', '$1$2', $_price);
This question already has answers here:
PHP substring extraction. Get the string before the first '/' or the whole string
(14 answers)
Closed 12 months ago.
I need to find a way in PHP to remove the last portions of 2 strings using regex's. This way once they are stripped of the extra characters I can find a match between them. Here is an example of the type of string data I am dealing with:
categories_widget-__i__
categories_widget-10
So I would like to remove:
-__i__ from the first string
-10 from the second string
Thanks in advance.
(.*)-
This simple regex can do your job if - is the splitting criteria
See demo.
http://regex101.com/r/rX0dM7/7
$str1 = "categories_widget-__i__";
$str2 = "categories_widget-10";
$arr1 = explode("-", $str1);
$arr2 = explode("-", $str2);
echo $arr1[0];
echo $arr2[0];
Is the last occurrence of a hyphen the only thing that's important? If so you don't need regex:
$firstPart = substr($str, 0, strrpos($str, '-'));
» example
You could try the below code to remove all the characters from - upto the last.
<?php
$text = <<<EOD
categories_widget-__i__
categories_widget-10
EOD;
echo preg_replace("~-.*$~m","",$text);
?>
Output:
categories_widget
categories_widget
- matches the literal - symbol. And .* matches any character following the - symbol upto the end of the line. $ denotes the end of a line. By replacing all the matched characters with an empty string would give you the desired output.
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