<?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, "Manuela", "Kant"),<br>(112233, "Johann", "Fichte"),<br>(145678, "Eduard", "Spinoza");</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();
You have javascript disabled. You will not be able to edit any code.