alexbts
alexbts

Reputation: 3

Regex in php - preg_replace issues

I would like to catch and replace a regex in a variable but I'm getting some troubles. The regex I'm trying to catch is a pattern like that :

one or many letters (one or many numbers (may have a .) one or many numbers) one or many letters

What I'm trying to do is to replace the whole string with only figures in it. Here's an example :

6 ° C => 6 1015.12 hPa => 1015.12 distance 172.1 km => 172.1

And here is my regex so far (don't blame me, I'm not really into regex haha) :

$test = preg_replace('#([a-zA-Z]*([0-9]*(\.)*[0-9]*)[a-zA-Z]*)#i', '$2', $myString);

Thanks in advance for you help !

Upvotes: 0

Views: 171

Answers (2)

alexbts
alexbts

Reputation: 3

Ok,

It seems I found the answer. My pattern wasn't correct, here's a good one :

'#[\D]([0-9](.)[0-9])[\D]*#i'

Upvotes: 0

Alfred
Alfred

Reputation: 21396

$result = preg_replace("/[^0-9,.]/","", $string);

Upvotes: 1

Related Questions