Reputation: 12047
I need to retrieve the first set of numbers from a string, but I'm not sure how.
I have the following, which I was expecting to pick each set of numbers so that I could then pick the first key from the $matches
array, but it literally matches only the first number.
In this example I'd be looking for '123'. Can someone please let me know how to do this with RegEx (or a better way if RegEx is not best for the job). Thanks.
$e = 'abc 123,456,def, 789-ab-552'; // Just a random example
$pattern = "/[0-9]/";
preg_match($pattern, $e, $matches);
Upvotes: 1
Views: 1702
Reputation: 89547
You must add a quantifier:
$pattern = "/[0-9]+/";
+
means one or more
You can find an ajax regex tester for php here and more informations about regular expressions here.
Upvotes: 3