Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the specific rules for using PHP regular expression preg_match?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail the specific rules for the use of PHP regular expression preg_match, and the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

PHP for beginners, the most difficult to understand is the application of regular expressions. Today we will introduce to you in detail the rules for the use of PHP regular expression preg_match, and hope that beginners can have a deep understanding of regular expression through the content introduced in this article.

The use of PHP regular expression preg_match:

With preg_match (), we can complete the rule matching of strings. If a match is found, the preg_match () function returns 1, otherwise 0. There is also an optional third parameter that allows you to put the matching parts in an array. This feature can be very useful when validating data.

$string = "football"; if (preg_match ('/ foo/', $string)) {/ / match correctly}

The above example will successfully match because the word football contains foo. Now let's try a more complex one, 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] {2pm 4} $/', $string))

{

/ / verify the Email address

}

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

Rules for the PHP regular expression preg_match:

Through the demonstration of the above example, we will understand the various rules represented by this regular expression.

As the name implies, PCRE has the same syntax as regular expressions in Perl, so each regular expression must have a pair of delimiters. We usually use the / delimiter.

The ^ at the beginning and the $at the end let PHP check from the beginning of the string to the end. If there is no $, the program will still match to the end of Email.

◆ [and] are used to restrict the type of license input. For example, Amurz allows all lowercase letters, Amurz allows all uppercase letters, 0-9 all numbers, and so on, and many other types.

◆ {and} are used to limit the expected number of characters. For example, {2pr 4} means that each section of a string can be 2-4 characters long, such as .com.cn or .info. Here it is. Does not count as a character, because the permission input type defined before {2recover4} has only uppercase and lowercase letters, so this paragraph matches only uppercase and lowercase letters

◆ (and) is used to merge sections and to define the characters that must exist in the string. (a | b | c) can match an or b or c.

◆ (.) All characters will be matched, and [.] Only match "." Itself.

To use some symbols themselves, one must be added in front of them. These characters are: () []. *? + ^ | $

The relevant content of PHP regular expression preg_match is introduced to you here. I hope it will help you to understand and master PHP preg_match regular expression.

On the PHP regular expression preg_match specific rules are shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report