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->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"> %ELEMENT% </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');

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.0110.00718.56
8.3.50.0120.00316.66
8.3.40.0060.00919.09
8.3.30.0070.00719.04
8.3.20.0050.00320.72
8.3.10.0040.00423.90
8.3.00.0080.00020.16
8.2.180.0070.01418.79
8.2.170.0110.01122.96
8.2.160.0100.01319.33
8.2.150.0080.00024.18
8.2.140.0090.00024.66
8.2.130.0040.00419.61
8.2.120.0040.00426.35
8.2.110.0040.00420.90
8.2.100.0080.00418.29
8.2.90.0040.00419.55
8.2.80.0000.00818.18
8.2.70.0000.00918.00
8.2.60.0000.00918.54
8.2.50.0060.00318.40
8.2.40.0000.00821.10
8.2.30.0000.01021.46
8.2.20.0050.00218.58
8.2.10.0000.00818.47
8.2.00.0030.00518.60
8.1.280.0090.00625.92
8.1.270.0080.00024.08
8.1.260.0080.00026.35
8.1.250.0000.00828.09
8.1.240.0000.01022.55
8.1.230.0040.00719.58
8.1.220.0030.00518.16
8.1.210.0000.00918.77
8.1.200.0060.00317.72
8.1.190.0000.00817.60
8.1.180.0000.00918.10
8.1.170.0030.00519.11
8.1.160.0000.00719.45
8.1.150.0000.00819.16
8.1.140.0080.00020.00
8.1.130.0000.00719.25
8.1.120.0000.00717.89
8.1.110.0040.00417.79
8.1.100.0030.00517.91
8.1.90.0030.00717.92
8.1.80.0080.00017.81
8.1.70.0080.00017.82
8.1.60.0040.00417.93
8.1.50.0000.00817.92
8.1.40.0080.00417.91
8.1.30.0000.00818.10
8.1.20.0000.00817.92
8.1.10.0000.00917.87
8.1.00.0040.00417.87
8.0.300.0060.00320.18
8.0.290.0000.00717.13
8.0.280.0000.00718.84
8.0.270.0070.00017.61
8.0.260.0030.00317.29
8.0.250.0000.00817.32
8.0.240.0080.00017.36
8.0.230.0040.00417.44
8.0.220.0000.00717.42
8.0.210.0000.00717.29
8.0.200.0000.00717.45
8.0.190.0000.00817.34
8.0.180.0070.00017.33
8.0.170.0030.00617.33
8.0.160.0000.00717.28
8.0.150.0050.00317.40
8.0.140.0050.00317.35
8.0.130.0000.00713.92
8.0.120.0000.00817.39
8.0.110.0080.00017.24
8.0.100.0040.00417.21
8.0.90.0030.00517.39
8.0.80.0100.01017.39
8.0.70.0040.00417.39
8.0.60.0000.00717.11
8.0.50.0000.00817.39
8.0.30.0080.01017.51
8.0.20.0100.01217.66
8.0.10.0050.00317.34
8.0.00.0090.00917.51
7.4.330.0000.00515.55
7.4.320.0040.00417.03
7.4.300.0000.00616.95
7.4.290.0070.00516.98
7.4.280.0070.00316.99
7.4.270.0000.00816.79
7.4.260.0040.00416.83
7.4.250.0050.00216.97
7.4.240.0000.00817.04
7.4.230.0050.00216.90
7.4.220.0170.00317.02
7.4.210.0090.00617.08
7.4.200.0000.00716.79
7.4.160.0130.00717.01
7.4.150.0100.01017.40
7.4.140.0080.01117.86
7.4.130.0090.01016.85
7.4.120.0100.01216.93
7.4.110.0140.00316.75
7.4.100.0230.02216.98
7.4.90.0090.00916.78
7.4.80.0030.01619.39
7.4.70.0050.01416.97
7.4.60.0030.01316.89
7.4.50.0100.00716.62
7.4.40.0090.00916.97
7.4.30.0070.01116.70
7.4.00.0070.01114.91
7.3.330.0000.00513.69
7.3.320.0030.00313.73
7.3.310.0000.00716.77
7.3.300.0030.00316.84
7.3.290.0120.00716.78
7.3.280.0080.00916.76
7.3.270.0060.01217.40
7.3.260.0070.01116.81
7.3.250.0100.01016.89
7.3.240.0090.00816.87
7.3.230.0070.01117.09
7.3.210.0100.00716.91
7.3.200.0100.01616.71
7.3.190.0030.01716.79
7.3.180.0000.01717.11
7.3.170.0120.00416.88
7.3.160.0180.00416.81
7.3.120.0100.01015.32
7.3.110.0060.01215.11
7.3.100.0030.01414.88
7.3.90.0100.00314.81
7.3.80.0030.01015.21
7.3.70.0060.00914.88
7.3.60.0030.01015.06
7.3.50.0060.00914.77
7.3.40.0000.01515.17
7.3.30.0030.01015.12
7.3.20.0090.00616.81
7.3.10.0070.00816.97
7.3.00.0040.01216.94
7.2.330.0090.01317.21
7.2.320.0060.01517.16
7.2.310.0100.00717.22
7.2.300.0100.00617.25
7.2.290.0140.00317.18
7.2.250.0030.01615.30
7.2.240.0070.01415.04
7.2.230.0060.00915.25
7.2.220.0090.00615.04
7.2.210.0060.00915.36
7.2.200.0040.00815.41
7.2.190.0120.00315.11
7.2.180.0040.01115.34
7.2.170.0060.00915.41
7.2.160.0070.00415.43
7.2.150.0070.01017.14
7.2.140.0110.00417.29
7.2.130.0040.01117.07
7.2.120.0040.01117.11
7.2.110.0060.00917.15
7.2.100.0080.00417.03
7.2.90.0060.01217.20
7.2.80.0080.00816.86
7.2.70.0090.00917.09
7.2.60.0130.00817.20
7.2.50.0140.00016.96
7.2.40.0040.01117.31
7.2.30.0110.00417.25
7.2.20.0070.01317.07
7.2.10.0070.01317.26
7.2.00.0040.01117.13
7.1.330.0070.01015.95
7.1.320.0110.00415.78
7.1.310.0070.00416.20
7.1.300.0030.00916.16
7.1.290.0120.00315.99
7.1.280.0000.01316.01
7.1.270.0000.01516.19
7.1.260.0030.01516.12
7.1.250.0040.01115.94
7.1.240.0060.00916.15
7.1.230.0070.00716.11
7.1.220.0100.00315.77
7.1.210.0100.00315.82
7.1.200.0060.01015.89
7.1.190.0030.01715.88
7.1.180.0060.01015.95
7.1.170.0070.01015.90
7.1.160.0040.00916.01
7.1.150.0030.01615.98
7.1.140.0120.00616.02
7.1.130.0090.00915.76
7.1.120.0060.01216.00
7.1.110.0030.00916.19
7.1.100.0030.00917.19
7.1.90.0140.00416.04
7.1.80.0120.00316.17
7.1.70.0430.01015.78
7.1.60.0500.01224.91
7.1.50.0880.00925.40
7.1.40.0170.01025.31
7.1.30.0480.01324.31
7.1.20.0360.01324.50
7.1.10.0080.01116.26
7.1.00.0250.00915.48
7.0.330.0040.00815.59
7.0.320.0000.00915.53
7.0.310.0070.00715.67
7.0.300.0090.00615.57
7.0.290.0070.00315.54
7.0.280.0060.00615.66
7.0.270.0000.00915.70
7.0.260.0100.00315.74
7.0.250.0040.00715.72
7.0.240.0070.01015.48
7.0.230.0100.00715.52
7.0.220.0050.00515.53
7.0.210.0070.00715.63
7.0.200.0280.01015.45
7.0.190.0100.00716.14
7.0.180.0350.01115.03
7.0.170.0380.01015.15
7.0.160.0080.01216.10
7.0.150.0250.01116.02
7.0.140.0260.01315.13
7.0.130.0250.01215.30
7.0.120.0250.01516.08
7.0.110.0220.01215.14
7.0.100.0250.00415.19
7.0.90.0230.00515.16
7.0.80.0300.00915.22
7.0.70.0290.01215.11
7.0.60.0030.01315.94
7.0.50.0090.00816.18
7.0.40.0100.00915.21
7.0.30.0070.00915.07
7.0.20.0080.00915.15
7.0.10.0040.01215.11
7.0.00.0110.00715.18
5.6.400.0090.00414.88
5.6.390.0000.01614.22
5.6.380.0060.00614.67
5.6.370.0060.01214.70
5.6.360.0000.01314.64
5.6.350.0040.00814.61
5.6.340.0100.01014.52
5.6.330.0060.00915.04
5.6.320.0070.01114.77
5.6.310.0070.01014.95
5.6.300.0030.00914.65
5.6.290.0000.01514.67
5.6.280.0080.00315.06
5.6.270.0060.01314.63
5.6.260.0100.00714.79
5.6.250.0120.00614.66
5.6.240.0060.00614.70
5.6.230.0040.00814.63
5.6.220.0030.01014.50
5.6.210.0030.01214.41
5.6.200.0030.01314.49
5.6.190.0030.01314.92
5.6.180.0030.01014.66
5.6.170.0000.01214.81
5.6.160.0120.00314.64
5.6.150.0060.00614.91
5.6.140.0120.00314.55
5.6.130.0000.01514.96
5.6.120.0080.00414.67
5.6.110.0070.00714.93
5.6.100.0040.00814.81
5.6.90.0060.00614.68
5.6.80.0070.01114.71
5.6.70.0110.00414.53
5.6.60.0060.00914.59
5.6.50.0110.00714.62
5.6.40.0000.00914.52
5.6.30.0000.01314.40
5.6.20.0080.00414.56
5.6.10.0130.00014.60
5.6.00.0030.01414.54

preferences:
40.23 ms | 400 KiB | 5 Q