Simple application of the PHP function preg_match_all regular expression. Before learning about regular expressions, you need to have some basic knowledge about common regular expressions. if you can remember these, you can only find them when you need them, before learning about regular expressions, you need to have basic knowledge about common regular expressions. if you can remember these, you can only find them when you need them, there are only a few special characters, so regular expressions are special. for details, refer to the more detailed description.
Preg_match_allFor more information about functions, see The PHP Manual.Preg_match_allUsed to test the regular expression effect.
Instance code:
$html = 'php100 php1002 php1003 '; |
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:
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 <或> Character, because if the two characters match, the following DIV will be matched, so you need to exclude the elements starting with these two characters, that is, do not match <或> Character, as follows:
Expression 2: [^ <>] + (Not matching <和> Characters)
In this way, the child expression to be matched is implemented, but
...