Reputation: 928
Hum.. I mean I can't make it work... So here is what I did..
$url = 'http://test/test/9244349';
$test = preg_match('#\d*#', $url, $matches);
array (size=1) 0 => string '' (length=0)
I just want to catch "9244349" but I don't figure out why it's not working.
Upvotes: 1
Views: 593
Reputation: 336158
Let's play regex engine:
\d*
means any number of digits. Including zero.h
.h
doesn't match \d
. \d
. So the empty string before h
matches.So, as mentioned in other posts, change the *
to +
to force at least one digit to match, then the rest will follow.
You could also use preg_match_all()
, but then you'd get a lot of empty matches, one for each position between two non-digits...
Upvotes: 1
Reputation: 76395
Try replacing *
with +
, if you want to match digits, there's no point in a pattern that matches, zero digits, too, is there?
preg_match('/\d+/','http://test/test/9244349',$matches);
echo $matches[0];//output:9244349
Upvotes: 1