0

I have the following

$data = "REFERENCE # 563005"
preg_match("/\d+/i", $data, $matches)
// $matches[0] will be set to 563005 with the above preg_match, however...

$data might be equal to "REFERENCE # 563005 & 563009 & 563125". In these cases, I need a regex to retrieve each instance of a number. So, doing a preg_match would set

$data = "REFERENCE # 563005 & 563009 & 563125"
preg_match("NEED REGEX", $data, $matches)
// $matches[0] would equal 563005, 
// $matches[1] would equal 563009, 
// $matches[2] would equal 563125, 

Depending on how many numers are in the string. These numbers may be of any length and there may be up to 10-20 of these numbers in the data string.

1
  • [\d]+[\D] i think would work, but you might have to remove the last character of the returned strinf Commented Jul 24, 2014 at 14:02

1 Answer 1

5

Instead of preg_match you need preg_match_all to match all the numbers in your input:

preg_match_all('/\d+/', $data, $matches);
Sign up to request clarification or add additional context in comments.

2 Comments

That did it, will accept your answer when SO lets me. Good to know there is a function built in to handle this.
Short and sweet, +1. @Angelo please remember to click the checkmark. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.