0

my PHP validation regex not working and i don't understand why. My code returning: 'Failure'. I checked my regex pattern and it is valid: https://regex101.com/r/2Is2L5/2

Phone value is: +7 (555) 555–55–55

if(preg_match('/^\+?\d{1,3}\s*\(\d{3}\)\s*\d{3}[-–•\s]?\d{2}[-–•\s]?\d{2}$/', $_GET['userPhone'])) {
    echo 'Success';
}
else{
    echo 'Failure';
}

Please help!

4
  • What is this character () ? Commented Feb 26, 2017 at 9:34
  • 1
    show var_dump($_GET['userPhone']); Commented Feb 26, 2017 at 9:34
  • • <= Is the alternate character, if you suddenly change design Commented Feb 26, 2017 at 9:39
  • var_dump is: string(22) "+7 (555) 555–55–55" Commented Feb 26, 2017 at 9:41

1 Answer 1

0

The following works for me:

$ cat a.php
<?php
if(preg_match('/^\+?\d{1,3}\s*\(\d{3}\)\s*\d{3}[-–•\s]?\d{2}[-–•\s]?\d{2}$/u', '+7 (555) 555–55–55')) {
    echo 'Success';
}
else{
    echo 'Failure';
}
?>

$ php a.php
Success

Notice the /u modifier at the end. According to the documentation:

u (PCRE_UTF8) This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern and subject strings are treated as UTF-8. An invalid subject will cause the preg_* function to match nothing; an invalid pattern will trigger an error of level E_WARNING. Five and six octet UTF-8 sequences are regarded as invalid since PHP 5.3.4 (resp. PCRE 7.3 2007-08-28); formerly those have been regarded as valid UTF-8.

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

1 Comment

Hey thank you! I just add "u" modifier at the end, and finally get Success!

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.