3v4l.org

run code in 300+ PHP versions simultaneously
<?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)); $newNode->parentNode->insertBefore($newTextNode, $newNode); } } }, $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> <div class="fourth">%ELEMENT%</div> <div class="fifth">a%ELEMENT%b</div> </body> </html> '); $parser = new MyParser($domdocu); $parser->insertInto('class', 'third', 'newtesttxtxttx'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'id', 'first'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'second'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'fourth'); $parser->replaceVariableInside('%ELEMENT%', 'TEEEXT', 'class', 'fifth'); echo $parser;
Output for 8.0.30, 8.1.22 - 8.1.28, 8.2.9 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
<!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"> b<span>TEEEXT</span>a </div> <div class="second"> <span>TEEEXT</span> <div class="third"> %ELEMENT% <span>newtesttxtxttx</span></div> </div> <div class="fourth"></div> <div class="fourth"><span>TEEEXT</span></div> <div class="fifth">a<span>TEEEXT</span>b</div> </body> </html>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 <!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"> b<span>TEEEXT</span>a </div> <div class="second"> <span>TEEEXT</span> <div class="third"> %ELEMENT% <span>newtesttxtxttx</span></div> </div> <div class="fourth"></div> <div class="fourth"><span>TEEEXT</span></div> <div class="fifth">a<span>TEEEXT</span>b</div> </body> </html>
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.29, 8.1.0 - 8.1.21, 8.2.0 - 8.2.8
<!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"> b<span>TEEEXT</span>a </div> <div class="second"> <span>TEEEXT</span> <div class="third"> %ELEMENT% <span>newtesttxtxttx</span></div> </div> <div class="fourth"></div> <div class="fourth"><span>TEEEXT</span></div> <div class="fifth">a<span>TEEEXT</span>b</div> </body></html>

preferences:
121.38 ms | 404 KiB | 173 Q