tags:

views:

77

answers:

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.

+4  A: 

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

Lauri Lehtinen
+10  A: 

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...

ircmaxell
A: 

Damn, you guys were too fast for me :( anyway this is what i was doing.

<?php
$string = "123456ABcd9999";
echo preg_replace('/[a-zA-Z]+/', '', $string);
?>

@a1anm you can also try this website out, http://gskinner.com/RegExr/ it is an online flash regEx where you can try out extractions and anything and there are lots of saved expressions from the community you can try aswell :)

Prix