简体   繁体   中英

Php regular expression to match a div

Foo Bar
 <div id="a">
   <div id="b">
    Foo
  </div>
   <div id="m">
   Bar
  </div>
 </div>

I want find all object in id="a" and out put is

  <div id="a"> <div id="b"> Foo </div> <div id="m"> Bar </div> </div> 

Geat thanks.

SimpleHTMLDom is memory consuming solution. It may cause memory leaks. I recommend using DOMDocument and DOMXPath . Then you can fetch all div#a by this way:

$xpath->query('//div[@id = "a"]')

Seems the community has responded to this before as a bad idea. Looks to be other, better ways. Jquery, HTML parsers, etc.

A search on regex in SO

StackOverflow Thoughts on Regex and HTML

PHP/HTML Regex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM