3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = ' <div> <p>Lorem Ipsum1</p> <p>Lorem Ipsum2</p> <h2>Title</h2> <p>Lorem Ipsum3</p><!-- PHP gets this paragraph --> <p>Lorem Ipsum4</p> <h2>Title</h2> <p>Lorem Ipsum5</p> </div>'; $xml = simplexml_load_string($str); $headings = $xml->xpath('h2'); $contents = []; foreach ($headings as $heading) { $contents[] = (string)$heading->xpath('following-sibling::p')[0]; } var_dump($contents);

preferences:
39.48 ms | 406 KiB | 5 Q