Before learning about regular expressions, you need to have some basic knowledge about common regular expressions. If you can remember these expressions, you can only find them when you need them, just a few special characters, therefore, regular expressions are special. For more details, see.
Preg_match_allFor more information about functions, see the PHP manual.Preg_match_allUsed to test the regular expression effect.
Instance code:
$html = '<div id="biuuu">php100</div><div id="biuuu_2">php1002</div><div id="biuuu_3">php1003</div>'; |
Instance requirements: retrieve the ID and content of each DIV element, such as biuuu, biuuu_2, biuuu_3, php100, php1002, and php1003 (some common site capture methods are matched in this way)
Analysis: A string is a simple HTML element. Each DIV element pair should have an ID and content independent. First, consider how to retrieve the ID value and content in a DIV, such: php100, and then match other similar elements. Two values must be extracted 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 ID content (php100 ), regular Expressions use parentheses, and the preceding elements are converted into the following forms:
<Div id = "(biuuu)"> (php100) </div> <div id = "(expression 1)"> (expression 2) </div> |
Well, use the parentheses above to divide the areas to be matched. Next we will explain how to match the content in each expression. We suppose an ID may be a letter, number, or underline, this becomes simple, and can be implemented using brackets, as shown below:
Expression 1: [A-zA-Z0-9 _] + (indicating matching upper and lower case letters, numbers and underscores)
Then how to match expression 2, because the ID content can be any character, but note that it cannot match <or> characters, if the two characters are matched, the following DIV will be matched. Therefore, you need to exclude the elements starting with these two characters, that is, do not match with <or> characters, as shown below:
Expression 2: [^ <>] + (Indicating unmatched <and> characters)
In this way, the child expression to be matched is implemented, but