3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<TAG <h1>This is my title</h1> <p>This is a text right under my h1 title.</p> <p>This is some more text under my h1 title</p> <h2>This is my level 2 heading</h2> <p>This is text right under my level 2 heading</p> <h3>First h3</h3> <p>First paragraph for the first h3</p> <h3>Second h3</h3> <p>First paragraph for the second h3</p> <h3>Third h3</h3> <p>First paragraph for the third h3</p> <p>Second paragraph for the third h3</p> <h2>This is my level 2 heading</h2> <p>This is text right under my level 2 heading</p> TAG; $dom = new DomDocument(); // Load the HTML, don't worry about it being a fragment $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); // Grab all H3 tags. This might need to be adjusted if there's more to the depth $results = $xpath->query("//h3"); foreach ($results as $result) { var_dump(sprintf('<h3>%1$s</h3>', $result->textContent)); // See if the next element is a P tag $next = $result->nextElementSibling; if ($next && 'p' === $next->nodeName) { var_dump(sprintf('<p>%1$s</p>', $next->textContent)); } }
Output for git.master, git.master_jit, rfc.property-hooks
string(17) "<h3>First h3</h3>" string(39) "<p>First paragraph for the first h3</p>" string(18) "<h3>Second h3</h3>" string(40) "<p>First paragraph for the second h3</p>" string(17) "<h3>Third h3</h3>" string(39) "<p>First paragraph for the third h3</p>"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
35.24 ms | 406 KiB | 5 Q