Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

preg_replace - strip unwanted characters from string to return numeric value

Tags:

string

regex

php

I hate regular expressions and I was hoping someone could help with a regualar expression to be used with preg_replace.

I want to strip unwanted characers from a string to return just a numeric value using preg_replace.

The string format could be as follows:

SOME TEXT £100

£100 SOME TEXT

SOME TEXT 100 SOME TEXT

Many thanks

like image 488
drs Avatar asked Jan 09 '11 13:01

drs


1 Answers

$NumericVal = preg_replace("/[^0-9]/","",$TextVariable);

the ^ inside the [ ] means anything except the following

Edit removed superfluous +

like image 176
Patrick Evans Avatar answered Oct 18 '22 18:10

Patrick Evans