Regular Expressions-in PHP, how to extract the specified HTML container with a regular

Source: Internet
Author: User
Tags preg
                

contont

ppppppp

my link

contont

ppppppp

my link

contont

ppppppp

my link

The above code, I need to extract all the contents of the container class baby, the structure is not necessarily the case. In other words, I need to extract the contents of a container. Thank you.

Reply content:

                

contont

ppppppp

my link

contont

ppppppp

my link

contont

ppppppp

my link

The above code, I need to extract all the contents of the container class baby, the structure is not necessarily the case. In other words, I need to extract the contents of a container. Thank you.

I don't know why everyone wants to use the regular to extract the DOM tree ... The regular is not used to do this ...

When you want to do this ... Do you see a full-heartedly grievance?

It is not good to let the maxima grind ... If you want to extract the DOM tree ... The right way is as follows ...


  
   loadHTML( <<
  
   

contont

ppppppp

my link

contont

ppppppp

my link

contont

ppppppp

my link HTML_SECTION);/* make a result array ... */$result = [];/* go through all nodes which have class="baby" ... */foreach( ( new DOMXPath( $doc ) )->query( '//*[@class="baby"]' ) as $element ) /* just push it into the result ... */ $result[] = $doc->saveHTML( $element );/* and print the result out ... */print_r( $result );

The rest of the DOM module I wrote in the previous answer is very clear ... Here is not to repeat the ...

If you are interested, you can take a look ...

As for the title of the question ... For more than 90% of the situation ... The answer is not to use the regular ...

If you insist that you do not need to use the regular ... And the document you are dealing with is the same as in your example ... There are ways to do so ...


  
   .*^\\1
  
   )ism',<<
  
   

contont

ppppppp

my link

contont

ppppppp

my link

contont

ppppppp

my link HTML_SECTION, $result_tmp );/* only the first element we need ... */print_r( array_shift( $result_tmp ) );

This method is only available on well-formatted HTML documents ... To determine the corresponding relationship of the label by indentation ...

If it's a confusing HTML document ... Then the regular is powerless to extract the DOM ...

nn ... That's it.

Try Phpquery, https://code.google.com/p/phpquery/.
Introduction can look at http://www.cnblogs.com/in-loading/archive/2012/04/11/2442697.html

$preg = '/\(.*?)\<\/div\>/s';preg_match_all($preg, $html, $match);

I don't know if that's okay ~

  • 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.