Regular Expression-in PHP, how to use regular expressions to extract the specified html container

Source: Internet
Author: User
Tags preg
{Code...} above, I need to extract all the container content whose class is baby. The structure is not necessarily like this. That is to say, I need to extract the content of a container. Thank you.

contont

ppppppp

my link

contont

ppppppp

my link

contont

ppppppp

my link

In the above code, I need to extract all the container content whose class is baby. The structure is not necessarily like this. That is to say, I need to extract the content of a container. Thank you.

Reply content:

contont

ppppppp

my link

contont

ppppppp

my link

contont

ppppppp

my link

In the above code, I need to extract all the container content whose class is baby. The structure is not necessarily like this. That is to say, I need to extract the content of a container. Thank you.

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

When you want to do this... Do you see regular expressions filled with grievances ..?

It is not good to make the cloudification of the Trojan horse... if you want to extract the DOM tree... the correct method 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 );

I have clearly written about other DOM modules in my previous answers... I will not repeat them here...

If you are interested, take a look...

As for the title question... for more than 90% cases... the answer isYou cannot use regular expressions....

If you insist that you must use regular expressions instead of regular expressions, and the documents you want to process are the same as those in your example... there is a method as follows...


  .*^\\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 applicable to well-formatted html documents... the indentation is used to determine the corresponding relationship of tags...

If it is a chaotic html document... Then regular expressions are powerless to extract DOM...

Grace... that's it...

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

$preg = '/\

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

I don't know if this works ~

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.