I hate regular expressions, and I was hoping someone could help with the regualar expression to be used with preg_replace.
I want to remove unnecessary characters from a string in order to return only a numeric value using preg_replace.
The format of the string can be as follows:
SOME TEXT ยฃ 100
ยฃ 100 SOME TEXT
SOME TEXT 100 SOME TEXT
Many thanks
$NumericVal = preg_replace("/[^0-9]/","",$TextVariable);
the ^ inside [] means everything except the following
Edit removed redundant +
+
$l = preg_replace("/[^A-Z0-9a-z\w ]/u", '', $l);
Works with UTF-8, only allows AZ az 0-9 ลwรณc ... etc.
preg_replace('/[^0-9]/','',$text);
Try the following:
preg_replace("/\D+/", "", "SOME TEXT ยฃ100")
You can also use preg_match to get the first number:
preg_match
preg_match("/\d+/", "SOME TEXT ยฃ100", $matches); $number = $matches[0];