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); $textNode->nextSibling->insertBefore($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 6eF27

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.2.60.0000.01516.83
7.2.00.0000.01519.17
7.1.200.0030.01016.11
7.1.100.0120.00018.45
7.1.70.0060.01517.34
7.1.60.0350.00935.24
7.1.50.0220.02534.78
7.1.40.0270.02134.53
7.1.30.0260.02034.60
7.1.20.0230.02334.72
7.1.10.0130.01016.70
7.1.00.0160.00316.77
7.0.200.0060.01016.78
7.0.190.0150.00517.12
7.0.180.0080.00816.47
7.0.170.0070.01716.48
7.0.160.0070.02016.57
7.0.150.0090.01316.59
7.0.140.0000.02316.56
7.0.130.0080.00816.64
7.0.120.0030.01516.79
7.0.110.0120.01116.55
7.0.100.0070.01316.54
7.0.90.0080.02016.32
7.0.80.0080.03116.71
7.0.70.0140.01316.52
7.0.60.0130.01216.43
7.0.50.0100.01416.65
7.0.40.0120.00916.61
7.0.30.0120.00616.58
7.0.20.0200.00416.54
7.0.10.0030.02216.71
7.0.00.0100.01416.65
hhvm-3.15.40.0900.21087.69
5.6.300.0190.09420.82
5.6.290.0300.05720.82
5.6.280.0200.06020.93
5.6.270.0240.06420.98
5.6.260.0180.06820.97
5.6.250.0180.08321.11
5.6.240.0350.08021.01
5.6.230.0220.08220.91
5.6.220.0230.08920.93
5.6.210.0310.06821.16
5.6.200.0220.08120.93
5.6.190.0130.07121.03
5.6.180.0210.06620.94
5.6.170.0200.07821.09
5.6.160.0100.07020.97
5.6.150.0190.07521.03
5.6.140.0220.05721.07
5.6.130.0200.07920.89
5.6.120.0200.08020.94
5.6.110.0290.07520.94
5.6.100.0300.05320.93
5.6.90.0170.07720.91
5.6.80.0220.05720.26
5.6.70.0320.07220.44
5.6.60.0220.07720.41
5.6.50.0140.08320.36
5.6.40.0130.06320.33
5.6.30.0100.08920.29
5.6.20.0130.08620.43
5.6.10.0300.07120.39
5.6.00.0130.06820.29