简体   繁体   中英

regular expression for phone number in php

I am using zend form validation for a phone number. and I want, user should enter phone number in these formats:

"+91-151-1234567", "01234567891", "+912345678901"

so i am using this regular expression:

"^(?:\+?([0-9]{3})\)?[-. ]?\(?([0-9]{3})\)?[-. ]?([0-9]{10})[-. ]$"

but it is not working. it is generating error:

Fatal error: Uncaught exception 'Zend_Validate_Exception' with message 'Internal error matching pattern '^(?:+?([0-9]{3}))?[-. ]?(?([0-9]{3}))?[-. ]?([0-9]{10})[-. ]$' against value '+91-151-3297154'' in /var/www/html/allindiazend/library/Zend/Validate/Regex.php:117 Stack trace: #0 /var/www/html/allindiazend/library/Zend/Validate.php(98): Zend_Validate_Regex->isValid('+91-151-3297154')

1 /var/www/html/allindiazend/library/Zend/Validate.php(98):

Zend_Validate->isValid('+91-151-3297154')

2 /var/www/html/allindiazend/library/Zend/Filter/Input.php(932):

Zend_Validate->isValid('+91-151-3297154')

3 /var/www/html/allindiazend/library/Zend/Filter/Input.php(800):

Zend_Filter_Input->_validateRule(Array)

4 /var/www/html/allindiazend/library/Zend/Filter/Input.php(688):

Zend_Filter_Input->_validate() #5 /var/www/html/allindiazend/library/Zend/Filter/Input.php(430): Zend_Filter_Input->_process() #6 /var/www/html/allindiazend/application/controllers/StaticController.php(148): Zend_Filter_Input->isValid() #7 /var/www/html/allindi in /var/www/html/allindiazend/library/Zend/Validate/Regex.php on line 117

Can any one help me for making this.

thank you in advance.

This expression will match all your 3 examples:

\+?([0-9]{2})-?([0-9]{3})-?([0-9]{6,7})

Not sure what part of yours isn't matching, but I just tested an alternative version I wrote and it seems to work for all your examples:

^(\+\d{12}|\d{11}|\+\d{2}-\d{3}-\d{7})$

Instead of doing extensive checking of all allowed formats, I would allow any format. You can convert it to a single format (without dashes and parentheses) do all the checking you want, and store each phone number in the same way, so that you can easily retrieve it as well.

If you solve your problem this way, your interface is more user friendly, additional checks are more easily added and your stored phone numbers are better usable for other applications.

Should I create a php script that extracts all the phone numbers listed on a page...

The numbers may be written in different formats such as: 0039024343333 +39024343333 (0041) 91 999 11 11 +41 (0) 91 999 11 11 0919991111 091 99 911 11 +1 123 344 2244 5 123-344-2244-5 etc..

I have tried this script but it works only in part:

GetPhoneNumber function ($ txt) {
$ regexp = '/ ([+ \ \ s]) {1,3} ([0-9 \ \ s] {2,5}) -? ([0-9 \ \ s] {2,5}) -? ([0-9 \ \ s] {2,20}) / ';

preg_match_all ($ regexp, $ txt, $ m);


return isset ($ m [0])? $ m [0]: array ();
}

$ fulltxt = file_get_contents ('http://wiki.wikimedia.it/wiki/Contatti');
$ phonenumber = GetPhoneNumber ($ fulltxt);
print_r ($ phonenumber);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM