How to use PHP function Preg_match_all to test regular expression effects _php tutorial

Source: Internet
Author: User
Tags nets
PHP function Preg_match_all The specific use of the method we canPHP function Preg_match_all Instance requirements: Separate the ID and content of each div element, such as biuuu,biuuu_2,biuuu_3,php self-study nets, PHP self-learning Nets 2 and PHP self-study Network 3 (some of the commonly used method of grasping the station is such a match)

Analysis: string is a simple HTML element, each DIV element pair should be an ID and content, and is independent, first of all, consider how to take out a div inside the ID value and content, such as: PHP Self-learning Network, and then match other similar elements. A DIV needs to take out two values, that is, two matching expressions, the first expression is used to match the ID value (BIUUU), the second expression is used to match the contents of the ID (PHP self-Learning Network), regular expressions commonly used expressions use parentheses, then the preceding elements will become the following form:

 
  
  
  1. < Span class= "Tag-name" >div
  2. id = "(biuuu)" >
  3. Li class= "alt" > (PHP self-study net)
  4. </ div >
  5. < div id = >
  6. (expression 2)
  7. </ div >

OK, using the parentheses to divide the areas that need to be matched, and then how to match the content within each expression, we suspect that an ID might be a letter, a number, or an underscore, and that's a simple thing to do with the brackets, as follows:

Expression 1:[a-za-z0-9_]+ (indicates matching case letters, numbers, and underscores)

How to match the expression 2, because the content of the ID can be any character, but note that the character cannot be matched <或> , because if the match between the two characters will be used to match the Div, so need to exclude the beginning of the two characters of the element, that is, do not match the <或> characters, as follows:

Expression 2:[^<>]+ (indicates mismatched <和> characters)

In this way, the PHP function Preg_match_all needs to match the sub-expression, but also need to match an expression, the method is as follows:

Expression:/' "(expression 1) ' > (expression 2)/

Note the double quotes "and/or the escape character escapes, and then put the preceding two expressions in the following:

 
  
  
  1. ' "([a-z0-9_]+)'> /
  2. Divid="([a-z0-9_]+)">
  3. ([^<>]+)
  4. </ Div > /

This implements a regular expression that matches each DIV element ID value and content, and then tests the following using the Preg_match_all function:

 
 
  1. $ HTML =
  2. ' < div id= ' biuuu ' >
  3. PHP Self-study web
  4. </div>
  5. < div id= "biuuu_2" >
  6. PHP Self-Learning Network 2
  7. </div>
  8. < div id= "Biuuu_3" >
  9. PHP Self-Learning Network 3
  10. </div> ' ;
  11. Preg_match_all ('/
  12. < Divsid Divsid = "([a-z0-9_]+)" >
  13. ([^<>]+)
  14. </ Div > /' , $html, $result);
  15. Var_dump ($result);

PHP function Preg_match_all Example results:

 
 
  1. Array (3) {
  2. [0]= > Array (3)
  3. {
  4. [0]= > string (30) "
    < Div id="biuuu">PHP Self-study net div> "
  5. [1]= > string (33) "
    < Div id="biuuu_2">PHP Self-learning Web 2 Div >"
  6. [2]= > string (33) "
    < Div id="biuuu_3">PHP Self-learning Web 3 Div >"
  7. }
  8. [1]= > Array (3)
  9. {
  10. [0]= > string (5) "Biuuu"
  11. [1]= > string (7) "Biuuu_2"
  12. [2]= > string (7) "Biuuu_3"
  13. }
  14. [2]= > Array (3)
  15. {
  16. [0]= > string (8) "PHP Self-study net"
  17. [1]= > string (9) "PHP Self-study net 2"
  18. [2]= > string (9) "PHP Self-study net 3"
  19. }
  20. }

There are three expressions, each showing the values matched by each expression and stored as an array, so that the ID and contents of each div element are taken out. The most important thing to use regular expressions is to know what needs to be, and then to match, and the idea is clear, and properly with the help of PHP function Preg_match_all output debugging, very convenient.


http://www.bkjia.com/PHPjc/446287.html www.bkjia.com true http://www.bkjia.com/PHPjc/446287.html techarticle PHP function Preg_match_all Specific use of the method we can in the PHP function Preg_match_all instance requirements: Separate the ID and content of each div element, such as biuuu,biuuu_2,biuuu_3,p ...

  • Related Article

    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.