3

I have this code:

$string = "123456ABcd9999"; 
$answer = ereg("([0-9]*)", $string, $digits); 
echo $digits[0]; 

This outputs '123456'. I'd like it to output '1234569999' ie. all the digits. How can I achieve this. I've been trying lots of different regex things but can't figure it out.

2 Answers 2

15

First, don't use ereg (it's deprecated). Secondly, why not replace it out:

$answer = preg_replace('#\D#', '', $string);

Note that \D is the inverse of \d. So \d matches all decimal numeric characters (0-9), therefore \D matches anything that \d does not match...

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

Comments

7

You could use preg_replace for this, preg_replace("/[^0-9]/", "", $string) for example.

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.