Home > Backend Development > PHP Tutorial > How to filter and validate user entered mobile phone number using PHP

How to filter and validate user entered mobile phone number using PHP

王林
Release: 2023-07-05 11:46:02
Original
1286 people have browsed it

How to use PHP to filter and verify user-entered mobile phone numbers

When developing web applications, it is often necessary to filter and verify user-entered data to ensure the accuracy and security of the data. Mobile phone numbers are one of the most common user-entered data. This article will introduce how to use PHP to filter and verify user-entered mobile phone numbers, and provide corresponding code examples.

  1. Use regular expressions to filter mobile phone numbers

In PHP, you can use regular expressions to filter and verify mobile phone numbers. Here is a sample code to filter out non-numeric characters and extract a valid mobile number:

$phone_number = $_POST['phone_number'];

// 使用正则表达式去除非数字字符
$filtered_number = preg_replace('/D/', '', $phone_number);

// 输出过滤后的手机号码
echo "过滤后的手机号码为: " . $filtered_number;
Copy after login

In the above code, we first get the user input from the form via $_POST mobile phone number. Then, use the preg_replace function and the regular expression /D/ to replace the non-numeric characters with an empty string to obtain the mobile phone number after removing the non-numeric characters.

  1. Use regular expressions to verify the mobile phone number format

In addition to filtering non-numeric characters, we also need to verify the mobile phone number format. The following is a sample code to verify whether the mobile phone number meets the format requirements for mainland China mobile phone numbers:

$phone_number = $_POST['phone_number'];

// 使用正则表达式验证手机号码格式
if (preg_match('/^1[3456789]d{9}$/', $phone_number)) {
    echo "手机号码格式正确";
} else {
    echo "手机号码格式不正确";
}
Copy after login

In the above code, we use the preg_match function and the regular expression /^1[3456789]d{9}$/Verify the mobile phone number. This regular expression can match 11 digits starting with 1, where the second digit can be any one of 3, 4, 5, 6, 7, 8, and 9, and the following 9 digits can be any number.

  1. Use the PHP library to verify mobile phone numbers

In addition to using regular expressions, you can also use the PHP library to verify mobile phone numbers. The following is a sample code that uses the libphonenumber library to verify mobile phone numbers:

require_once 'vendor/autoload.php';

use libphonenumberPhoneNumberUtil;

$phone_number = $_POST['phone_number'];

$phoneUtil = PhoneNumberUtil::getInstance();

try {
    $numberProto = $phoneUtil->parse($phone_number, 'CN');
    if ($phoneUtil->isValidNumber($numberProto)) {
        echo "手机号码有效";
    } else {
        echo "手机号码无效";
    }
} catch (libphonenumberNumberParseException $e) {
    echo "无法解析手机号码";
}
Copy after login

In the above code, we first introduced the libphonenumber library and used PhoneNumberUtil::getInstance()Get an PhoneNumberUtil instance. Then, use the parse method to parse the mobile phone number into a PhoneNumber object, and use the isValidNumber method to verify whether the mobile phone number is valid.

Summary:

This article introduces how to use PHP to filter and verify the mobile phone number entered by the user. By using regular expressions or PHP libraries, developers can implement filtering and format verification of mobile phone numbers to improve data accuracy and security.

Hope this article is helpful to you!

The above is the detailed content of How to filter and validate user entered mobile phone number using PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template