3v4l.org

run code in 500+ PHP versions simultaneously
<?php $html = <<<'EOD' <div> <p> Here the inline code to parse: `code = 'set';` - Danke. </p> <p> And here is the other part: </p> <p> <code>CREATE table `kunstler` (<br>id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,<br>vorname VARCHAR(255) NOT NULL,<br>nachname VARCHAR(255) NOT NULL<br>) DEFAULT CHARSET=utf8;<br><br>INSERT INTO `kunstler` (id, vorname, nachname)<br>VALUES<br>(123456, &quot;Manuela&quot;, &quot;Kant&quot;),<br>(112233, &quot;Johann&quot;, &quot;Fichte&quot;),<br>(145678, &quot;Eduard&quot;, &quot;Spinoza&quot;);</code> </p> </div> EOD; $dom = new DOMDocument; $dom->loadHTML($html, LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); $xp = new DOMXPath($dom); $textNodes = $xp->query('//text()[not(ancestor::code)][contains(.,"`")]'); function genTextParts($text) { foreach (explode('`', $text) as $part) { yield $part; } } foreach ($textNodes as $textNode) { $parts = genTextParts($textNode->nodeValue); $frag = $dom->createDocumentFragment(); do { $frag->appendChild($dom->createTextNode($parts->current())); $parts->next(); if ( $parts->valid() ) { $codeElt = $dom->createElement('code'); $codeElt->appendChild($dom->createTextNode($parts->current())); $frag->appendChild($codeElt); $parts->next(); } } while ($parts->valid()); $textNode->parentNode->replaceChild($frag, $textNode); } echo $dom->saveHTML();

preferences:
21.83 ms | 475 KiB | 4 Q