Introduction to the specific usage rules of PHP regular expression preg_match_PHP tutorial

WBOY
Release: 2016-07-15 13:30:10
Original
1127 people have browsed it

Usage of PHP regular expression preg_match:

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

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">string</span><span> = </span><span class="attribute-value">"football"</span><span>;</span></span></li>
<li class="alt"><span><span> if (preg_match('/foo/', $string)) </span></span></li>
<li class="alt"><span><span>{ </span></span></li>
<li class="alt"><span><span>// 匹配正确 </span></span></li>
<li class="alt"><span><span>}  </span></span></li>
</ol>
Copy after login

The above example will match successfully because the word football contains foo. Now let's try something more complex, such as validating an email address.

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">string</span><span> = </span><span class="attribute-value">"first.last@domain.uno.dos"</span><span>; </span></span></li>
<li class="alt"><span><span>if (preg_match( '/^[^0-9][a-zA-Z0-9_]+ <br>([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+ ([.]<br>[a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $string)) </span></span></li>
<li class="alt"><span><span>{ </span></span></li>
<li class="alt"><span><span>// 验证Email地址 </span></span></li>
<li class="alt"><span><span>} </span></span></li>
</ol>
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 can understand the various rules represented by this regular expression.

PCRE, as the name suggests, 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 leading ^ and trailing $ 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 the allowed 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 segment only matches uppercase and lowercase letters

◆( and) are used to merge section 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: ( ) [ ] . * ? + ^ | $

The related content of PHP regular expression preg_match is introduced to you here. I hope it will be helpful for you to understand and master PHP preg_match regular expression. .


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446307.htmlTechArticleUsage of PHP regular expression preg_match: Using preg_match(), we can complete the rule matching of strings. The preg_match() function returns 1 if a match is found, 0 otherwise. ...
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