Angelo
Angelo

Reputation: 427

php regex for two number strings within a string

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.

Upvotes: 0

Views: 33

Answers (1)

anubhava
anubhava

Reputation: 785246

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

preg_match_all('/\d+/', $data, $matches);

Upvotes: 5

Related Questions