PHP function preg_match_all regular expression simple application _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
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:

(Php100)

(Expression 2)

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

...

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.