0

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 !

2
  • so you only want to keep the numbers and letters where they belong? Commented Apr 8, 2011 at 9:30
  • What result do you exactly want? Commented Apr 8, 2011 at 9:30

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you !! I didn't think to think that way (delete everything which is not interessing) !
then close your question please
@alexbts, if this answer satisfied your needs you should accept it by activating the check mark on the left of this answer.
0

Ok,

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.