0

i am using the following code to detect if a string is a phone number...

<?php
$text = "[email protected]" ;
if (preg_match_all('/\+?[0-9][0-9()-\s+]{4,20}[0-9]/',$text)){
    echo "phone number";    
}else{
    echo "not a phone number";
}
?>

the problem is that i get phone number where as i should be getting not a phone number... how can i solve this problem... any help would be appreciated.

6
  • You want to check if someone has used a phone number in their email address? That's not gonna work too well. Commented Dec 13, 2013 at 18:39
  • i mean phone number instead of an email address... Commented Dec 13, 2013 at 18:41
  • Take a look at stackoverflow.com/questions/3357675/… Commented Dec 13, 2013 at 18:44
  • What if someone had an email address as `peterpan201344 Commented Dec 13, 2013 at 18:44
  • yeah that's the problem.. @AmalMurali Commented Dec 13, 2013 at 18:50

3 Answers 3

2

The problem is that {4,20} will match any string in which the preceding block appears 4 to 20 times, so it matches the digits in the sample email address. Try this:

<?php
$text = "[email protected]" ;
if (preg_match_all('/^\+?([0-9-]|\s|\([0-9]+\)){4,20}[0-9]/',$text)){
    echo "phone number";    
}else{
    echo "not a phone number";
}
?>

Regular expression visualization

Debuggex Demo

Note that preg_match_all requires 3 parameters in PHP < 5.4.

Sign up to request clarification or add additional context in comments.

2 Comments

your code works fine but the problem is i get not a phone number when i put in this +8801689347804
That's because I didn't know you wanted to match + signs. I've updated it to match an optional + at the beginning.
1

preg_match_all() starts from the first match until the last one.

In this case the first match would be 4, and it continues till 6. Ignoring all the other characters.

To make it start from the beginning use ^ (beginning of line anchor) before the regular expression. And to force it to continue until the end put $ (end of line anchor) at the end of the regular expression.

Like this:

<?php
$text = "[email protected]" ;
if (preg_match_all('/^+?[0-9][0-9()-\s+]{4,20}[0-9]/$', $text)) {
  echo "phone number";  
} else{
  echo "not a phone number";
}
?>

Comments

0

If I understand your problem correctly, the regular expression you've got in your example is returning a match even when someone puts in an email address with numbers, like you have there.

Match to the beginning and end of the line only to avoid this, using ^ and $:

<?php
$text = "[email protected]" ;
if (preg_match_all('^/\+?[0-9][0-9()-\s+]{4,20}[0-9]$/',$text)){
    echo "phone number";    
}else{
    echo "not a phone number";
}
?>

Working example: http://regex101.com/r/xY0yU3

Comments

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.