<?php $html = <<<EOT <body> <div class="page-content"> First text <p>Second text</p> <div>Third text</div> <p>More text</p> <h4>Subtitle 1</h4> <p>bla bla</p> <p>bla bla</p> <h4>Subtitle 2</h4> <p>bla bla</p> <p>bla bla</p> </div> </body> EOT; $doc = new DOMDocument(); $doc->loadHTML($html); // Get our node by class name // See https://stackoverflow.com/a/6366390/231316 $finder = new DomXPath($doc); $classname = "page-content"; $nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]"); $buf = ''; foreach ($nodes as $node) { foreach ($node->childNodes as $child) { if ($child->nodeName === 'h4') { break; } $buf .= $doc->saveHTML($child); } } echo $buf;
You have javascript disabled. You will not be able to edit any code.