Home  >  Article  >  Backend Development  >  How to use regular expressions in PHP to verify input of mobile phone number and email address

How to use regular expressions in PHP to verify input of mobile phone number and email address

王林
王林Original
2023-06-25 14:17:17914browse

With the continuous popularity and use of the Internet in our daily lives, it is becoming more and more common to enter and verify users' contact information, such as mobile phone numbers and email addresses. Therefore, it becomes even more important to validate these inputs to ensure that your application or website only accepts valid inputs.

As a very popular server-side scripting language, PHP’s powerful regular expression capabilities make it very suitable for such verification tasks. In this article, we will demonstrate how to use regular expressions in PHP to validate input of mobile phone numbers and email addresses.

Verify mobile number

Verifying the correct format of a mobile number is very simple using regular expressions in PHP. Here is a sample code that can verify a number that matches the Chinese mainland mobile phone number format:

<?php
$phone_number = "13800138000";
if (!preg_match("/^1[3-9][0-9]{9}$/", $phone_number)) {
    echo "Invalid phone number.";
}
else {
    echo "Valid phone number.";
}
?>

Let's take a closer look at the above code. We define a variable $phone_number to save the entered mobile phone number. We then match it against a regular expression using the preg_match() function.

Regular expression ^1[3-9][0-9]{9}$ is used to match numbers that conform to the format of mainland Chinese mobile phone numbers. The meaning of this regular expression is: starting with the number 1, followed by numbers from 3 to 9, followed by 9 any numbers.

If the input $phone_number successfully matches the regular expression, then "Valid phone number" will be output. If no match is successful, "Invalid phone number" will be output.

Validate Email Address

The regular expression for validating email addresses is a little more complicated. Here is a sample code that can verify that the email address format matches most situations:

<?php
$email = "example@example.com";
if (!preg_match("/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,}$/", $email)) {
    echo "Invalid email address.";
}
else {
    echo "Valid email address.";
}
?>

Similarly, we define a variable $email to hold the entered email address. We then match it against a regular expression using the preg_match() function.

Regular expression/^[A-Za-z0-9._% -] @[A-Za-z0-9.-] .[A-Za-z]{2, }$/ is a commonly used regular expression for email validation. This regular expression means: an email address begins with one or more letters, numbers, periods, underscores, percent signs, plus signs, dashes, followed by an @ symbol, followed by one or more letters , any combination of numbers, periods, dashes, and a top-level domain name (any character with two or more letters).

If the entered $email matches the regular expression successfully, "Valid email address" will be output. If no match is successful, "Invalid email address" will be output.

Summary

Regular expressions are a very effective way to verify input formats. As a popular server-side language, PHP has powerful regular expression capabilities. By using regular expressions, you can easily verify mobile phone numbers and email addresses that match a specific format.

It should be noted that regular expressions can not only be used to verify solemn input, but can also be used for many other operations, such as string search, replacement and splitting. Understanding the tools and syntax of regular expressions can bring more flexibility and efficiency to your programming work.

The above is the detailed content of How to use regular expressions in PHP to verify input of mobile phone number and email address. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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