Get html between comment block Simple HTM DOM

How can I take a DOM block by specifying its tag 'comment', for example

<!-- start block --> <p>Hello world etc</p> <div>something</div> <!-- end of block --> 

I use a simple PHP DOM parser, but the document is incomplete, http://simplehtmldom.sourceforge.net/manual.htm . It is ok if I can do this with pure PHP.

+6
source share
1 answer

You can try iterating over the elements first, and then if you find the original comment, skip it first and then add a flag that will start concatenating the following elements. If you reach the endpoint, stop the concatenation:

 $html_string = '<!-- start block --> <p>Hello world etc</p> <div>something</div> <div>something2</div> <!-- end of block --> <div>something3</div> '; $html = str_get_html($html_string); // start point $start = $html->find('*'); $output = ''; $go = false; foreach($start as $e) { if($e->innertext === '<!-- start block -->') { $go = true; continue; } elseif($e->innertext === '<!-- end of block -->') { break; } if($go) { $output .= $e; } } echo $output; 
+1
source

Source: https://habr.com/ru/post/985680/


All Articles