Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use php to determine whether it is a mobile phone number or not

Shulou Source: shulou.com Published: 2022-06-03 05:16:39 10月03日 Update

This article introduces the relevant knowledge of "how to use php to judge whether it is a mobile phone number or not". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The method of php judging whether it is a mobile phone number: 1, using the regular expression "$g =" / ^ 1 [34578]\ d {9} $/ ";" judging the mobile phone number; 2, judging by "if (preg_match (" / ^ 1 [34578]\ d {9} $/ ", $mobile)) {...}".

This article operating environment: windows7 system, PHP7.1 version, DELL G3 computer

The method of judging Mobile phone number regularly by php

Introduction:

Php uses regular expressions to determine how mobile phone numbers are written: match all the mobile phone numbers from the article and preg_match_all (). If you want to check whether the mobile phone number entered by the user is correct, check it like this: preg_match ().

When using regular matching mobile phone numbers, let's first analyze the rules of mobile phone numbers:

1. The mobile phone number is usually 11 digits.

two。 It often starts with 1.

3. The second number is usually the number 34578, and the mobile phone number in section 170 goes on sale on May 5, 2014, so there is an extra 7.

Php uses regular expressions to determine how to write a mobile phone number:

G = "/ ^ 1 [34578]\ d {9} $/"

The / / "^" symbol must start with 1; "[]" means that the second number must be a number in square brackets; and\ d means any number in square brackets, followed by {9} that the length is 9 digits; the following $indicates the end; the regular expressions at the beginning and end must be placed in the middle, and some may be followed by a pattern.

Match all the mobile phone numbers from the article and write like this:

$str = 'Old Bar blogger's mobile phone is 18912345678 (/ ^ 1 [34578]\ d {9} $/ ", $str, $mobiles); / / var_dump ($mobiles)

If you want to check whether the mobile phone number entered by the user is correct, check it like this:

If (preg_match ("/ ^ 1 [34578]\ d {9} $/", $mobile)) {/ / there is an infinite imagination} "how to use php to determine whether it is a mobile phone number". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Mobile phone mobile phone number mobile phone number number regular expression check article content writing beginning method more user knowledge input practical learned next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Docker Redmi Xiaomi vpn