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)); $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;

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).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0080.00818.77
8.3.50.0120.00316.66
8.3.40.0040.01119.09
8.3.30.0000.01518.97
8.3.20.0040.00420.53
8.3.10.0080.00024.04
8.3.00.0030.00520.04
8.2.180.0110.01118.76
8.2.170.0080.00822.96
8.2.160.0040.01119.41
8.2.150.0030.00524.18
8.2.140.0080.00024.66
8.2.130.0000.00819.75
8.2.120.0050.00326.35
8.2.110.0100.00021.42
8.2.100.0090.00318.41
8.2.90.0000.00819.72
8.2.80.0030.00518.43
8.2.70.0040.00418.13
8.2.60.0030.00618.41
8.2.50.0050.00318.54
8.2.40.0000.00821.02
8.2.30.0000.00819.58
8.2.20.0000.00818.39
8.2.10.0000.00718.44
8.2.00.0000.00818.62
8.1.280.0110.00425.92
8.1.270.0080.00022.70
8.1.260.0090.00026.35
8.1.250.0030.00628.09
8.1.240.0090.00024.27
8.1.230.0110.00019.52
8.1.220.0040.00418.16
8.1.210.0060.00318.77
8.1.200.0090.00017.85
8.1.190.0030.00617.73
8.1.180.0000.00818.10
8.1.170.0000.00819.00
8.1.160.0030.00619.32
8.1.150.0070.00019.29
8.1.140.0080.00019.99
8.1.130.0000.00719.29
8.1.120.0040.00417.94
8.1.110.0030.00517.82
8.1.100.0050.00317.75
8.1.90.0000.00817.89
8.1.80.0050.00317.87
8.1.70.0080.00017.88
8.1.60.0000.00817.90
8.1.50.0040.00417.86
8.1.40.0090.00517.93
8.1.30.0040.00417.98
8.1.20.0060.00317.91
8.1.10.0030.00617.93
8.1.00.0050.00317.88
8.0.300.0040.00420.27
8.0.290.0030.00617.25
8.0.280.0050.00318.89
8.0.270.0050.00217.61
8.0.260.0000.00717.31
8.0.250.0000.00817.45
8.0.240.0000.00717.45
8.0.230.0040.00417.43
8.0.220.0040.00417.29
8.0.210.0000.00717.40
8.0.200.0030.00317.45
8.0.190.0030.00617.48
8.0.180.0060.00317.40
8.0.170.0000.00717.25
8.0.160.0040.00417.31
8.0.150.0040.00417.32
8.0.140.0020.00517.25
8.0.130.0030.00313.77
8.0.120.0000.00817.35
8.0.110.0000.00717.31
8.0.100.0030.00617.52
8.0.90.0050.00317.30
8.0.80.0090.00917.42
8.0.70.0080.00017.27
8.0.60.0030.00517.40
8.0.50.0050.00317.40
8.0.30.0070.01217.48
8.0.20.0220.01217.50
8.0.10.0040.00417.55
8.0.00.0070.01017.24
7.4.330.0030.00315.55
7.4.320.0040.00416.88
7.4.300.0000.00616.95
7.4.290.0090.00316.98
7.4.280.0080.00016.89
7.4.270.0040.00417.07
7.4.260.0070.00016.91
7.4.250.0030.00517.00
7.4.240.0020.00517.04
7.4.230.0000.00717.08
7.4.220.0070.01117.04
7.4.210.0090.01317.00
7.4.200.0000.00716.71
7.4.160.0060.00916.89
7.4.150.0060.01217.40
7.4.140.0130.00817.86
7.4.130.0090.00917.18
7.4.120.0120.01116.94
7.4.110.0060.01216.97
7.4.100.0090.00816.96
7.4.90.0090.01516.86
7.4.80.0060.01319.39
7.4.70.0090.00916.91
7.4.60.0090.01516.90
7.4.50.0050.00717.20
7.4.40.0150.00316.76
7.4.30.0060.00917.00
7.4.00.0070.01115.28
7.3.330.0060.00013.69
7.3.320.0000.00513.77
7.3.310.0040.00416.63
7.3.300.0000.00716.75
7.3.290.0080.00716.74
7.3.280.0100.00816.76
7.3.270.0140.00717.40
7.3.260.0110.01116.96
7.3.250.0090.00916.89
7.3.240.0130.00516.89
7.3.230.0110.00717.06
7.3.210.0150.00316.86
7.3.200.0080.01216.81
7.3.190.0080.01117.02
7.3.180.0100.01017.13
7.3.170.0130.00317.05
7.3.160.0110.00616.83
7.2.330.0180.00416.96
7.2.320.0040.01516.90
7.2.310.0150.00617.20
7.2.300.0110.00616.88
7.2.290.0070.01117.04
7.2.60.0060.01017.13
7.1.200.0060.00615.92
7.1.70.0410.01215.57
7.1.60.0640.01633.68
7.1.50.0590.01335.16
7.1.40.0560.01432.81
7.1.30.0710.01334.63
7.1.20.0470.02532.93
7.1.10.0330.01115.00
7.1.00.0100.01016.64
7.0.200.0420.01115.00
7.0.190.0040.01516.93
7.0.180.0120.01216.47
7.0.170.0100.01616.38
7.0.160.0100.01516.56
7.0.150.0120.01016.57
7.0.140.0060.01516.48
7.0.130.0040.01516.85
7.0.120.0030.01316.65
7.0.110.0100.01316.54
7.0.100.0110.01016.48
7.0.90.0090.01216.48
7.0.80.0080.01316.70
7.0.70.0070.01416.45
7.0.60.0070.01416.30
7.0.50.0070.01416.52
7.0.40.0100.01516.75
7.0.30.0110.00916.64
7.0.20.0100.01116.40
7.0.10.0090.01316.57
7.0.00.0060.01316.46

preferences:
36.77 ms | 400 KiB | 5 Q