Preg_replace - remove unnecessary characters from a string to return a numeric value

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

+6
string php regex
source share
4 answers
$NumericVal = preg_replace("/[^0-9]/","",$TextVariable); 

the ^ inside [] means everything except the following

Edit removed redundant +

+16
source share
 $l = preg_replace("/[^A-Z0-9a-z\w ]/u", '', $l); 

Works with UTF-8, only allows AZ az 0-9 ล‚wรณc ... etc.

+8
source share
 preg_replace('/[^0-9]/','',$text); 
0
source share

Try the following:

 preg_replace("/\D+/", "", "SOME TEXT ยฃ100") 

You can also use preg_match to get the first number:

 preg_match("/\d+/", "SOME TEXT ยฃ100", $matches); $number = $matches[0]; 
0
source share

All Articles