Home >Backend Development >PHP Tutorial >Simple application of PHP function preg_match_all regular expression_PHP tutorial

Simple application of PHP function preg_match_all regular expression_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:26:05712browse

Before understanding regular expressions, you need to master some basic knowledge of commonly used regular expressions. If you can remember these, it is best to remember them. If you can’t remember, you can look them up when you need to use them. Just a few special ones. Characters, so regular expressions are special. You can view more detailed instructions for details.

preg_match_allFor detailed description of the function, you can check the PHP manual. This article uses preg_match_all to test the effect of regular expressions.

Example code:

$html = '<div id="biuuu">php100</div><div id="biuuu_2">php1002</div><div id="biuuu_3">php1003</div>';

Example requirements: Take out the ID and content of each DIV element, such as biuuu, biuuu_2, biuuu_3, php100, php1002 and php1003 (Some commonly used website grabbing methods are matched in this way)

Analysis : The string is a simple HTML element, and each DIV element corresponds to an ID and content, and is independent First, consider how to extract the ID value and content within a DIV, such as: php100, and then match other similar elements. Two values ​​need to be taken out from a DIV, that is, two matching expressions. The first expression is used to match the ID value (biuuu), and the second expression is used to match the content of the ID (php100). Regular expression Commonly used expressions use parentheses, then the previous elements will become the following form:

<div id="(biuuu)">(php100)</div> <div id="(表达式1)">(表达式2)</div>

Okay, use the above parentheses to divide the areas that need to be matched. The next step is how to match each expression. Content, we guess that an ID may be letters, numbers or underscores, then this becomes simple. It can be achieved using square brackets, as follows:

Expression 1: [a- zA-Z0-9_]+ (indicates matching uppercase and lowercase letters, numbers and underscores)

How to match expression 2, because the content of the ID can be any character, but please note that it cannot match < or > character, because if you match these two characters, all DIVs used later will be matched, so you need to exclude elements starting with these two characters, that is, do not match the < or > characters, as follows:

Expression 2: [^<>]+ (indicating that the < and > characters are not matched)

In this way, the subexpression that needs to be matched is realized, But you also need to match a

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446628.htmlTechArticleBefore understanding regular expressions, you need to master some basic knowledge of commonly used regular expressions. If you can remember these, it is best to It’s easy to remember. Just look it up when you need to use it. Just a few special features...
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