Simple Application of the PHP function preg_match_all Regular Expression

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.