5

i am having a small problem with my regex which i use to extract phone numbers from a strong

<?php
$output = "here 718-838-3586 there 1052202932 asdas dasdasd 800-308-4653 dasdasdasd 866-641-6949800-871-0999";
preg_match_all('/\b[0-9]{3}\s*[-]?\s*[0-9]{3}\s*[-]?\s*[0-9]{4}\b/',$output,$matches);
echo '<pre>';
print_r($matches[0]);
?>

output

Array
(
            [0] => 718-838-3586
            [1] => 1052202932
            [2] => 800-308-4653
            [3] => 866-641-6949
            [4] => 800-871-0999

)

this work fine but it returns 1052202932 as one of result which i don't need .
actually i don't know where is the missing part in my pattern .

1
  • Your pattern? Did you write it? Then just make the dashes not optional again. Commented Jun 24, 2013 at 22:10

2 Answers 2

2

The ? after each [-] is making the - optional. If you want it to be required you can just remove the ? which will make it required. Also, [-] is equivalent to - so I got rid of the unnecessary character class:

preg_match_all('/\b[0-9]{3}\s*-\s*[0-9]{3}\s*-\s*[0-9]{4}\b/',$output,$matches);

You can also replace all of the [0-9] with \d to shorten it a bit further:

preg_match_all('/\b\d{3}\s*-\s*\d{3}\s*-\s*\d{4}\b/',$output,$matches);
Sign up to request clarification or add additional context in comments.

Comments

2

? in regex means {0,1} and you need exactly 1 occurence of '-' in your pattern

preg_match_all('/\b[0-9]{3}\s*-\s*[0-9]{3}\s*-\s*[0-9]{4}\b/',$output,$matches);

For more info http://www.php.net/manual/en/regexp.reference.repetition.php

3 Comments

+ is not the same as {1}, it is the same as {1,}. (so this would match 123----456----7890)
there fixed, slight error to merit a down vote wouldn't you think
Not really, I have never seen a phone number with multiple adjacent dashes.

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.