Home > Backend Development > PHP Tutorial > Detailed explanation of preg_match function in php regular expressions

Detailed explanation of preg_match function in php regular expressions

黄舟
Release: 2023-03-17 11:00:02
Original
13549 people have browsed it

We have previously introduced you to the use, getting started, and verifying email addresses of php regular expressions, so today we would like to introduce to you the function preg_match in php regular expressions , what are the rules of preg_match function in PHP regular expressions?

Usage of PHP regular expression preg_match function:

Using preg_match(), you can complete the rule matching of string . The preg_match() function returns 1 if a match is found, 0 otherwise. There is also an optional third parameter that allows you to store the matching part in an array. This feature is very important and useful when validating data.

$string = "php爱好者";
 if (preg_match('/php/', $string))
{
// 匹配正确
}
Copy after login

The above example will match successfully because "php lovers" contains php. Now let's try something more complex, such as validating an email address.

$string = "first.last@domain.uno.dos";
if (preg_match( '/^[^0-9][a-zA-Z0-9_]+
([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+ ([.]
[a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $string))
{
// 验证Email地址
}
Copy after login

This example will verify that the email address is in the correct format.


Rules for PHP regular expression preg_match:

Through the demonstration of the above example, we will understand the various rules represented by this regular expression.
PCRE As the name suggests, it has the same syntax as regular expressions in Perl, so each regular expression must have a pair of delimiters. We generally use / as the delimiter.
The ^ at the beginning and the $ at the end tell PHP to check from the beginning to the end of the string. Without the $, the program will still match to the end of the Email.
◆[ and ] are used to limit permission input types. For example a-z allows all lowercase letters, A-Z allows all uppercase letters, 0-9 all numbers, etc., and many more.
◆{ and } are used to limit the number of characters expected. For example, {2,4} means that each section of the string can be 2-4 characters long, such as .com.cn or .info. Here, "." does not count as a character, because the allowed input type defined before {2,4} only has uppercase and lowercase letters, so this paragraph only matches uppercase and lowercase letters
◆(and) are used to merge sections, and Defines the characters that must be present in the string. (a|b|c) matches a or b or c.
◆(.) will match all characters, while [.] will only match "." itself.
To use some symbols themselves, you must add a in front. These characters are: ( ) [ ] . * ? + ^ | $

Summary:

preg_match() function in php regular expressions Matching will be performed in , and the matching will stop after the first successful matching. If you want to achieve matching of all results, I hope it will be helpful to your work!

Related recommendations:

php regular expression verification email address case


Instance analysis of php regular expressions


##Detailed introduction to php regular expressions


The ten most practical PHP regular expressions

The above is the detailed content of Detailed explanation of preg_match function in php regular expressions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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