First, do not use ereg (it is deprecated). Secondly, why not replace it:
$answer = preg_replace('#\D#', '', $string);
Please note that this \Dis the opposite of \D. So it \Dmatches all decimal numeric characters (0-9), so it \Dmatches all those that \Ddon't match ...
source
share