PHP RegEx - Get All Digits

I have this code:

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

This displays "123456". I would like it to give out '1234569999', i.e. all numbers. How can i achieve this. I tried many different things, but I can not understand.

+3
source share
2 answers

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

+7
source

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

+14
source

Source: https://habr.com/ru/post/1749979/


All Articles