PHP Extract the phone number in the string implementation code

Source: Internet
Author: User
This article is mainly to share the PHP extract string in the mobile phone number implementation code, PHP through the regular expression of the string to extract the phone number and judge the operator, simple and quick and convenient, can extract multiple mobile phone number. The following through the example code to give you a detailed introduction, the needs of the Friends of the reference bar hope to help everyone.

1. Code

<?phpheader ("content-type:text/plain;charset=utf-8"); function findthephonenumbers ($oldStr = "") {//Detect string is empty $  Oldstr=trim ($OLDSTR);  $numbers = Array ();  if (empty ($OLDSTR)) {return $numbers;  }//delete the middle of a number such as 86-180640741122,0997-8611222 minus (-) $STRARR = Explode ("-", $oldStr);  $NEWSTR = $STRARR [0]; for ($i =1; $i < count ($STRARR), $i + +) {if (Preg_match ("/\d{2}$/", $newStr) && preg_match ("/^\d{11}/", $str     arr[$i]) {$newStr. = $STRARR [$i]; } elseif (Preg_match ("/\d{3,4}$/", $newStr) && preg_match ("/^\d{7,8}/", $strArr [$i])) {$newStr. = $STRARR [$i     ];     } else {$newStr. = "-". $STRARR [$i]; }}//The acquisition of the mobile number $reg = '/\d (?: 86)?  (\d{11}) \d/is ';//match number of regular expression Preg_match_all ($reg, $NEWSTR, $result);  $nums = Array (); * Mobile: China Mobile//* 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188 $cm = "/^1 (34[0-8]| (  3[5-9]|5[017-9]|8[278] \d) \d{7}$/"; * Unicom: China Unicom//* 130,131,132,152,155,156,185,186 $CU = "/^1 (3[0-2]|5[256]|8[56]) \d{8}$/";  * China Telecom: Telecom//* 133,1349,153,180,189 $ct = "/^1 ((33|53|8[09]) [0-9]|349] \d{7}$/";  foreach ($result [1] as $key = + $value) {if (Preg_match ($cm, $value)) {$nums [] = Array ("number" = = $value,    "Type" = "China Mobile");    }elseif (Preg_match ($CU, $value)) {$nums [] = Array ("number" = = $value, "type" = "China Unicom");    }elseif (Preg_match ($CT, $value)) {$nums [] = Array ("number" = = $value, "type" = "China Telecom");  }else{//Illegal Number}} $numbers ["mobile"] = $nums;  Fixed phone or phs get $reg = '/\d (0\d{10,12}) \d/is ';//match number of regular expression Preg_match_all ($reg, $NEWSTR, $result);  $nums = Array (); * Mainland Landline or PHS//* Area code: 010,020,021,022,023,024,025,027,028,029//* Number: seven-bit or eight-bit $phs = "/^0 (10|2[0-5789]|\d{3}) \d{7,8}$  /"; foreach ($result [1] as $key = + $value) {if (Preg_match ($phs, $value)) {$nums [] = Array ("number" + = $value, "    Type "= =" Fixed telephone or phs ");  } else {//illegal}} $numbers ["landline"] = $nums; There could be no access to landline phones without area code.  $reg = '/\d (\d{7,8}) \d/is ';//Regular expression Preg_match_all matching numbers ($reg, $newStr, $result);  $nums = Array ();  foreach ($result [1] as $key + $value) {$nums [] = Array ("Number" and "* * * * * $value," type "=" fixed phone without area code ");  } $numbers ["possible"] = $nums; Return the final array return $numbers;} Test Data $str = "This (8625010) is a number, and the Another are here (09978625000), the phone number is 18064074452 and 138995 55555. This is Chinese, there is a 13239323232 mobile phone number, there is a 188779988441 this is 12 bit 8613322114455. Here again is a cell phone number 86-18064074455. There is also an area code separate write 0997-8625001hahaha "; $nums = Findthephonenumbers ($str);p rint_r ($nums);

2. Test results

array ([mobile] = = Array ([0] = = Array ([number] =&G T          18064074452 [Type] = China Telecom) [1] = = Array ([number] = 13899555555        [Type] = China Mobile) [2] = = Array ([number] = 13239323232 [Type] = + Unicom ) [3] = = Array ([number] = 13322114455 [Type] = China Telecom) [4] =&G T      Array ([number] = 18064074455 [Type] = + China Telecom)) [landline] = = Array ( [0] = = Array ([number] = 09978625000 [Type] = = Fixed telephone or PHS) [1] = Ar       Ray ([number] = 09978625001 [Type] = = Fixed phone or PHS)] [possible] = = Array ( [0] = = Array ([number] = 8625010 [Type] + = fixed telephone with no area code))) 

3. Results analysis

Mobile mobile phone number landline fixed phone or phs possible may be a landline with no area code
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.