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 */ if(@$pos && $pos !== false) { $newTextNode = $newTextNode->cloneNode(true); } $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;

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.0070.01518.81
8.3.50.0080.00817.13
8.3.40.0090.00619.12
8.3.30.0150.00019.26
8.3.20.0040.00420.71
8.3.10.0030.00524.04
8.3.00.0060.00320.06
8.2.180.0110.01118.79
8.2.170.0090.00622.96
8.2.160.0150.00019.61
8.2.150.0030.00524.18
8.2.140.0000.00824.66
8.2.130.0000.00819.68
8.2.120.0000.00826.35
8.2.110.0100.00321.33
8.2.100.0090.00318.24
8.2.90.0090.00019.77
8.2.80.0000.00818.30
8.2.70.0060.00318.25
8.2.60.0030.00618.53
8.2.50.0040.00418.43
8.2.40.0040.00420.97
8.2.30.0000.00819.63
8.2.20.0080.00018.45
8.2.10.0000.00818.45
8.2.00.0080.00018.52
8.1.280.0070.01325.92
8.1.270.0090.00022.52
8.1.260.0050.00326.35
8.1.250.0000.00928.09
8.1.240.0060.00322.56
8.1.230.0030.01021.13
8.1.220.0000.00818.14
8.1.210.0040.00418.77
8.1.200.0030.00717.85
8.1.190.0040.00417.73
8.1.180.0030.00518.10
8.1.170.0000.00818.98
8.1.160.0080.00019.34
8.1.150.0000.00819.21
8.1.140.0050.00320.04
8.1.130.0000.00719.30
8.1.120.0000.00717.91
8.1.110.0030.00617.80
8.1.100.0000.00717.87
8.1.90.0030.00517.91
8.1.80.0040.00417.90
8.1.70.0030.00517.72
8.1.60.0030.00617.94
8.1.50.0030.00617.92
8.1.40.0030.01017.98
8.1.30.0000.00818.03
8.1.20.0000.00818.11
8.1.10.0030.00517.88
8.1.00.0060.00317.93
8.0.300.0090.00020.13
8.0.290.0090.00017.25
8.0.280.0040.00418.90
8.0.270.0040.00417.69
8.0.260.0000.00817.41
8.0.250.0050.00217.45
8.0.240.0030.00517.51
8.0.230.0000.00717.34
8.0.220.0040.00417.41
8.0.210.0000.00717.30
8.0.200.0030.00317.38
8.0.190.0060.00317.37
8.0.180.0050.00317.47
8.0.170.0000.00817.34
8.0.160.0040.00417.41
8.0.150.0050.00317.35
8.0.140.0020.00517.40
8.0.130.0030.00313.80
8.0.120.0050.00317.32
8.0.110.0040.00417.38
8.0.100.0050.00317.39
8.0.90.0040.00417.35
8.0.80.0140.00717.32
8.0.70.0000.00917.41
8.0.60.0000.00817.22
8.0.50.0040.00417.33
8.0.30.0130.00517.58
8.0.20.0120.01017.53
8.0.10.0040.00417.38
8.0.00.0110.00717.31
7.4.330.0030.00315.55
7.4.320.0070.00016.94
7.4.300.0000.00616.90
7.4.290.0030.01017.10
7.4.280.0080.00016.96
7.4.270.0070.00017.04
7.4.260.0020.00517.09
7.4.250.0060.00316.80
7.4.240.0030.00516.91
7.4.230.0070.00017.06
7.4.220.0030.01616.92
7.4.210.0060.00917.02
7.4.200.0000.00717.12
7.4.160.0100.01016.96
7.4.150.0120.00617.40
7.4.140.0080.01117.86
7.4.130.0130.00617.09
7.4.120.0090.01117.08
7.4.110.0070.01016.69
7.4.100.0150.00816.93
7.4.90.0070.01117.00
7.4.80.0130.00719.39
7.4.70.0140.00817.13
7.4.60.0040.01916.83
7.4.50.0040.01117.06
7.4.40.0100.00716.90
7.4.30.0090.00917.00
7.4.00.0120.00515.18
7.3.330.0030.00313.75
7.3.320.0030.00313.77
7.3.310.0030.00516.89
7.3.300.0070.00016.75
7.3.290.0100.00616.83
7.3.280.0070.01116.89
7.3.270.0050.01417.40
7.3.260.0070.01016.91
7.3.250.0050.01216.93
7.3.240.0110.00916.85
7.3.230.0060.01216.91
7.3.210.0090.00916.97
7.3.200.0200.00317.13
7.3.190.0130.00316.89
7.3.180.0070.01016.90
7.3.170.0030.01316.97
7.3.160.0070.01016.85
7.3.120.0080.00815.14
7.3.110.0060.01214.84
7.3.100.0090.00615.14
7.3.90.0000.01014.86
7.3.80.0090.00314.89
7.3.70.0060.01015.13
7.3.60.0090.00915.14
7.3.50.0080.00014.86
7.3.40.0080.00814.90
7.3.30.0030.01015.02
7.3.20.0040.01116.69
7.3.10.0090.00616.87
7.3.00.0030.00616.76
7.2.330.0060.01317.12
7.2.320.0120.00617.17
7.2.310.0080.00816.75
7.2.300.0060.00917.09
7.2.290.0190.00416.88
7.2.250.0040.01115.26
7.2.240.0100.01015.52
7.2.230.0040.01415.27
7.2.220.0060.01215.55
7.2.210.0030.00715.13
7.2.200.0040.01115.29
7.2.190.0000.01315.29
7.2.180.0090.00615.23
7.2.170.0070.00315.36
7.2.60.0040.01117.09
7.2.00.0000.01619.84
7.1.330.0080.00615.94
7.1.320.0100.00316.00
7.1.310.0090.00616.08
7.1.300.0030.00916.00
7.1.290.0100.00715.94
7.1.280.0060.00315.97
7.1.270.0070.00615.98
7.1.260.0040.00815.89
7.1.200.0000.01516.03
7.1.100.0110.00818.20
7.1.70.0030.01617.11
7.1.60.0260.01935.18
7.1.50.0330.01034.77
7.1.40.0160.01934.68
7.1.30.0250.01934.61
7.1.20.0180.01534.64
7.1.10.0030.01316.69
7.1.00.0030.01916.93
7.0.200.0140.00616.80
7.0.190.0140.00617.08
7.0.180.0070.01116.49
7.0.170.0070.01416.41
7.0.160.0110.00816.45
7.0.150.0140.00816.38
7.0.140.0020.01816.52
7.0.130.0070.01416.65
7.0.120.0070.01016.73
7.0.110.0080.01416.47
7.0.100.0120.00716.43
7.0.90.0100.01616.48
7.0.80.0120.01016.59
7.0.70.0100.01316.47
7.0.60.0150.01016.45
7.0.50.0030.01916.53
7.0.40.0070.01516.63
7.0.30.0100.01216.61
7.0.20.0130.00516.59
7.0.10.0140.00616.54
7.0.00.0080.01116.60

preferences:
42.96 ms | 401 KiB | 5 Q