3v4l.org

run code in 200+ PHP versions simultaneously
<?php $html = '<html><body><p>This is a <em>test</em>.</p></body></html>'; $dom = new domdocument; $dom->loadhtml($html); $p = $dom->getelementsbytagname('p')[0]; $text = ''; foreach ($p->childNodes as $node) { if ($node->nodeName === '#text') { $text .= $node->textContent; } if ($node->nodeName === 'em') { $text .= "*{$node->textContent}*"; } } echo $text;
based on k6K6q
Output for 7.1.25 - 7.3.5
This is a *test*.