开发者

PHP RegEx - Get All Digits

I have this code:

$string = "123456ABcd9999"; 
$answer = ereg("([0-9]*)", $string, $digits); 
ech开发者_JS百科o $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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜