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 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.4, 8.3.6 - 8.3.27, 8.4.1 - 8.4.14
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>"
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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>"
Output for 7.3.13 - 7.3.33, 7.4.0 - 7.4.33
string(17) "<h3>First h3</h3>" Notice: Undefined property: DOMElement::$nextElementSibling in /in/gvBrv on line 39 string(18) "<h3>Second h3</h3>" Notice: Undefined property: DOMElement::$nextElementSibling in /in/gvBrv on line 39 string(17) "<h3>Third h3</h3>" Notice: Undefined property: DOMElement::$nextElementSibling in /in/gvBrv on line 39

preferences:
156.31 ms | 410 KiB | 5 Q