3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class MyParser { private $domdoc; public function __construct(DOMDocument $domdoc){ $this->domdoc = $domdoc; } public function __toString() { return $this->domdoc->saveHTML(); } public function replaceVariableInside($varName, $text, $idType, $idValue) { $nodes = $this->getNodes($idType, $idValue); $newTextNode = $this->getNodeFromText($text); array_map(function($node) use ($varName, $newTextNode){ /* @var $node DOMNode */ $textNodes = array_filter($this->dnl2array($node->childNodes), function($node){ return $node->nodeType === XML_TEXT_NODE; }); foreach ($textNodes as $textNode) { /* @var $textNode \DOMText */ $pos = strpos($textNode->nodeValue, $varName); if($pos !== false) { $newNode = $textNode->splitText($pos); $newNode->nodeValue = substr($newNode->nodeValue, strlen($varName)); var_dump($newTextNode); $newNode->parentNode->appendChild($newTextNode); } } }, $nodes); } protected function getNodes($idType, $idValue) { switch ($idType){ case 'id': return [ $this->domdoc->getElementById($idValue) ]; break; case 'class': return $this->dnl2array((new DOMXPath($this->domdoc)) ->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $idValue ')]")); break; default: break; } } public function insertInto($idType, $id, $text) { $newnode = $this->getNodeFromText($text); array_map(function($elementOfDom) use ($newnode){ /* @var $elementOfDom DOMNode */ @$elementOfDom->appendChild($newnode); }, $this->getNodes($idType, $id)); } protected function getNodeFromText($text) { $text = html_entity_decode($text); try { $tmp = new DOMDocument; $textDocumentRes = @$tmp->loadHTML('<?xml encoding="utf-8" ?><span>'.$text.'</span>'); } catch(Exception $e) { $textDocumentRes = false; } if($textDocumentRes) { $newnode = $this->domdoc->importNode($tmp->documentElement->firstChild->firstChild, true); } else { $newnode = $this->domdoc->createTextNode($text); $newnode->nodeValue = $text; } return $newnode; } private function dnl2array($domnodelist) { $return = array(); $length = $domnodelist->length; for ($i = 0; $i < $length; ++$i) { $return[] = $domnodelist->item($i); } return $return; } } $domdocu = new DomDocument(); $domdocu->loadHTML(' <html> <head> </head> <body> <div id="first"> b%ELEMENT%a </div> <div class="second"> %ELEMENT% <div class="third"> %ELEMENT% </div> </div> <div class="fourth"> %ELEMENT% </div> </body> </html> '); $parser = new MyParser($domdocu); $parser->insertInto('class', 'third', 'newtesttxtxttx'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'id', 'first'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'second'); echo $parser;
based on NkVAE
Output for 7.3.0alpha1 - 7.3.0rc3
object(DOMElement)#7 (18) { ["tagName"]=> string(4) "span" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "span" ["nodeValue"]=> string(6) "TEEEXT" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "span" ["baseURI"]=> NULL ["textContent"]=> string(6) "TEEEXT" } object(DOMElement)#3 (18) { ["tagName"]=> string(4) "span" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "span" ["nodeValue"]=> string(6) "TEEEXT" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "span" ["baseURI"]=> NULL ["textContent"]=> string(6) "TEEEXT" } <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><head></head><body> <div id="first"> ba <span>TEEEXT</span></div> <div class="second"> <div class="third"> %ELEMENT% <span>newtesttxtxttx</span></div> <span>TEEEXT</span></div> <div class="fourth"> %ELEMENT% </div> </body></html>
Output for 7.0.16 - 7.0.20, 7.1.2 - 7.2.11
object(DOMElement)#7 (18) { ["tagName"]=> string(4) "span" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "span" ["nodeValue"]=> string(6) "TEEEXT" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "span" ["baseURI"]=> NULL ["textContent"]=> string(6) "TEEEXT" } object(DOMElement)#6 (18) { ["tagName"]=> string(4) "span" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "span" ["nodeValue"]=> string(6) "TEEEXT" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "span" ["baseURI"]=> NULL ["textContent"]=> string(6) "TEEEXT" } <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><head></head><body> <div id="first"> ba <span>TEEEXT</span></div> <div class="second"> <div class="third"> %ELEMENT% <span>newtesttxtxttx</span></div> <span>TEEEXT</span></div> <div class="fourth"> %ELEMENT% </div> </body></html>
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.0.15, 7.1.0 - 7.1.1
object(DOMElement)#7 (17) { ["tagName"]=> string(4) "span" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "span" ["nodeValue"]=> string(6) "TEEEXT" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "span" ["baseURI"]=> NULL ["textContent"]=> string(6) "TEEEXT" } object(DOMElement)#6 (17) { ["tagName"]=> string(4) "span" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "span" ["nodeValue"]=> string(6) "TEEEXT" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "span" ["baseURI"]=> NULL ["textContent"]=> string(6) "TEEEXT" } <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><head></head><body> <div id="first"> ba <span>TEEEXT</span></div> <div class="second"> <div class="third"> %ELEMENT% <span>newtesttxtxttx</span></div> <span>TEEEXT</span></div> <div class="fourth"> %ELEMENT% </div> </body></html>
Output for hhvm-3.15.4
object(DOMElement)#15 (18) { ["nodeName"]=> string(4) "span" ["nodeValue"]=> string(6) "TEEEXT" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "span" ["baseURI"]=> NULL ["textContent"]=> string(6) "TEEEXT" ["tagName"]=> string(4) "span" ["schemaTypeInfo"]=> NULL } object(DOMElement)#26 (18) { ["nodeName"]=> string(4) "span" ["nodeValue"]=> string(6) "TEEEXT" ["nodeType"]=> int(1) ["parentNode"]=> NULL ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> NULL ["nextSibling"]=> NULL ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "span" ["baseURI"]=> NULL ["textContent"]=> string(6) "TEEEXT" ["tagName"]=> string(4) "span" ["schemaTypeInfo"]=> NULL } <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><head></head><body> <div id="first"> ba <span>TEEEXT</span></div> <div class="second"> <div class="third"> %ELEMENT% <span>newtesttxtxttx</span></div> <span>TEEEXT</span></div> <div class="fourth"> %ELEMENT% </div> </body></html>