phppreg-match

Make preg_match() match all occurrences in text


I want to extract some phone numbers that are in a local text file on my hard drive. I'm thinking that I can use preg_match for this. I've got the following that manages to pull the first phone number then stops. Do I need a loop or what am I missing to get all of the phone numbers? I'm not even sure that my regex is correct. The phone numbers are all in the format: xxx-xxx-xxxx

$file = file_get_contents('1.txt');
preg_match('/([0-9]){3}-([0-9]){3}-([0-9]){4}/', $file, $matches);
print_r($matches);

Solution

  • Did you notice a similar function named preg_match_all()?