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($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 (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; } } $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.70.0110.00718.77
8.3.60.0090.00618.93
8.3.50.0100.00916.89
8.3.40.0000.01519.21
8.3.30.0110.00719.18
8.3.20.0040.00420.66
8.3.10.0060.00323.89
8.3.00.0090.00019.80
8.2.180.0070.01418.91
8.2.170.0180.00322.96
8.2.160.0140.00719.31
8.2.150.0070.00025.66
8.2.140.0050.00324.66
8.2.130.0080.00026.16
8.2.120.0050.00320.46
8.2.110.0110.00021.07
8.2.100.0060.00618.22
8.2.90.0060.00319.32
8.2.80.0000.00818.43
8.2.70.0030.00718.00
8.2.60.0000.00918.30
8.2.50.0030.00618.54
8.2.40.0080.00020.94
8.2.30.0000.00821.28
8.2.20.0000.00818.52
8.2.10.0000.00818.56
8.2.00.0070.00018.55
8.1.280.0040.01125.92
8.1.270.0000.00924.09
8.1.260.0080.00026.35
8.1.250.0080.00028.09
8.1.240.0060.00322.51
8.1.230.0100.00319.54
8.1.220.0000.00817.92
8.1.210.0060.00318.77
8.1.200.0000.00917.85
8.1.190.0000.00917.85
8.1.180.0040.00418.16
8.1.170.0080.00418.95
8.1.160.0040.00419.43
8.1.150.0080.00019.35
8.1.140.0040.00419.91
8.1.130.0070.00019.30
8.1.120.0000.00717.95
8.1.110.0080.00017.76
8.1.100.0050.00317.80
8.1.90.0040.00417.79
8.1.80.0040.00417.84
8.1.70.0040.00417.90
8.1.60.0060.00318.04
8.1.50.0040.00417.89
8.1.40.0030.01217.94
8.1.30.0040.00418.05
8.1.20.0030.00618.11
8.1.10.0090.00017.95
8.1.00.0000.00917.94
8.0.300.0030.00620.32
8.0.290.0060.00317.13
8.0.280.0040.00418.88
8.0.270.0040.00417.71
8.0.260.0000.00819.28
8.0.250.0000.00717.49
8.0.240.0050.00217.40
8.0.230.0030.00517.36
8.0.220.0050.00217.29
8.0.210.0040.00417.40
8.0.200.0000.00717.27
8.0.190.0040.00417.38
8.0.180.0000.00717.29
8.0.170.0000.00817.44
8.0.160.0070.00017.48
8.0.150.0000.00717.32
8.0.140.0030.00617.27
8.0.130.0000.00613.80
8.0.120.0040.00417.37
8.0.110.0070.00017.35
8.0.100.0040.00417.44
8.0.90.0080.00017.27
8.0.80.0130.00917.32
8.0.70.0040.00417.14
8.0.60.0000.00817.31
8.0.50.0040.00417.27
8.0.30.0140.00717.49
8.0.20.0100.01017.57
8.0.10.0050.00317.22
8.0.00.0090.00917.25
7.4.330.0030.00315.55
7.4.320.0000.00717.03
7.4.300.0070.00017.01
7.4.290.0090.00617.03
7.4.280.0030.00616.98
7.4.270.0000.00716.93
7.4.260.0030.00617.04
7.4.250.0080.00016.95
7.4.240.0040.00416.79
7.4.230.0030.00317.04
7.4.220.0110.00716.95
7.4.210.0060.00917.01
7.4.200.0000.00816.95
7.4.160.0120.00516.97
7.4.150.0080.01217.40
7.4.140.0080.01217.86
7.4.130.0130.00616.93
7.4.120.0110.01217.02
7.4.110.0090.00917.07
7.4.100.0090.01317.11
7.4.90.0070.01116.82
7.4.80.0150.00319.39
7.4.70.0060.01216.75
7.4.60.0030.01316.86
7.4.50.0060.00617.07
7.4.40.0040.01316.89
7.4.30.0090.00816.98
7.4.10.0090.00915.21
7.4.00.0080.00915.11
7.3.330.0050.00013.83
7.3.320.0000.00613.79
7.3.310.0000.00716.79
7.3.300.0000.00716.77
7.3.290.0060.01016.87
7.3.280.0080.00916.81
7.3.270.0090.01217.40
7.3.260.0130.00716.96
7.3.250.0080.01116.90
7.3.240.0140.00316.98
7.3.230.0140.00916.84
7.3.210.0130.00616.99
7.3.200.0070.01416.88
7.3.190.0130.00316.88
7.3.180.0110.00617.10
7.3.170.0100.00716.90
7.3.160.0040.01416.88
7.3.130.0090.00615.20
7.3.120.0060.01215.04
7.3.110.0050.01514.94
7.3.100.0050.01015.18
7.3.90.0030.01215.16
7.3.80.0050.00515.10
7.3.70.0030.00915.14
7.3.60.0020.00715.03
7.3.50.0050.00815.12
7.3.40.0050.00715.14
7.3.30.0040.01315.14
7.3.20.0100.00616.82
7.3.10.0040.01316.83
7.3.00.0060.00616.83
7.2.330.0110.00816.88
7.2.320.0090.00917.15
7.2.310.0090.00917.01
7.2.300.0050.01116.80
7.2.290.0040.02016.94
7.2.260.0030.01615.08
7.2.250.0070.01315.26
7.2.240.0080.00915.27
7.2.230.0060.01015.41
7.2.220.0060.00815.33
7.2.210.0030.01015.05
7.2.200.0080.00615.27
7.2.190.0030.00815.34
7.2.180.0040.01015.31
7.2.170.0030.00815.29
7.2.160.0160.00015.17
7.2.150.0120.00316.82
7.2.140.0130.00317.20
7.2.130.0060.00717.24
7.2.120.0070.00617.19
7.2.110.0070.00817.21
7.2.100.0100.00517.16
7.2.90.0030.01217.10
7.2.80.0070.00717.04
7.2.70.0050.01017.05
7.2.60.0040.00816.96
7.2.50.0060.00717.08
7.2.40.0060.00717.23
7.2.30.0080.00517.05
7.2.20.0070.00917.08
7.2.10.0030.00916.93
7.2.00.0060.00917.96
7.1.330.0110.00515.97
7.1.320.0080.00516.00
7.1.310.0000.01316.00
7.1.300.0050.01215.89
7.1.290.0050.01016.00
7.1.280.0050.00515.90
7.1.270.0100.00515.88
7.1.260.0080.00516.00
7.1.250.0040.00815.95
7.1.240.0040.01215.80
7.1.230.0030.01015.97
7.1.220.0000.01416.10
7.1.210.0060.00916.04
7.1.200.0070.00216.07
7.1.190.0070.00716.20
7.1.180.0100.00715.98
7.1.170.0000.01416.10
7.1.160.0040.00815.84
7.1.150.0030.01015.95
7.1.140.0100.00716.15
7.1.130.0000.01215.98
7.1.120.0060.00815.82
7.1.110.0030.01415.93
7.1.100.0060.00817.08
7.1.90.0030.00616.00
7.1.80.0070.01016.11
7.1.70.0080.00916.65
7.1.60.0230.01125.51
7.1.50.0150.00925.25
7.1.40.0210.00825.31
7.1.30.0150.01325.44
7.1.20.0110.01825.40
7.1.10.0070.01316.38
7.1.00.0090.00916.39
7.0.330.0090.00615.66
7.0.320.0030.01015.38
7.0.310.0070.00715.57
7.0.300.0070.01115.66
7.0.290.0070.00715.56
7.0.280.0000.01515.42
7.0.270.0060.00915.57
7.0.260.0060.00915.41
7.0.250.0090.00315.73
7.0.240.0150.00015.67
7.0.230.0060.00915.60
7.0.220.0070.01015.80
7.0.210.0060.00615.72
7.0.200.0130.00616.32
7.0.190.0040.01316.32
7.0.180.0060.00915.91
7.0.170.0060.01216.06
7.0.160.0070.00915.90
7.0.150.0020.01915.92
7.0.140.0090.01015.94
7.0.130.0110.00816.09
7.0.120.0080.00916.16
7.0.110.0080.01015.92
7.0.100.0030.01316.08
7.0.90.0080.00916.03
7.0.80.0080.01116.13
7.0.70.0020.01715.96
7.0.60.0040.01015.97
7.0.50.0060.01215.97
7.0.40.0060.01315.11
7.0.30.0040.01215.03
7.0.20.0100.00914.98
7.0.10.0060.00815.15
7.0.00.0050.01015.16
5.6.400.0090.00314.86
5.6.390.0040.00814.45
5.6.380.0000.02114.89
5.6.370.0060.00914.78
5.6.360.0000.00914.62
5.6.350.0070.00314.64
5.6.340.0040.00714.54
5.6.330.0060.00914.22
5.6.320.0070.00714.80
5.6.310.0130.00314.65
5.6.300.0040.01214.56
5.6.290.0000.01414.33
5.6.280.0070.00714.29
5.6.270.0030.00714.41
5.6.260.0140.00314.95
5.6.250.0100.00714.54
5.6.240.0070.01014.49
5.6.230.0100.00014.48
5.6.220.0060.00914.95
5.6.210.0090.00614.47
5.6.200.0030.01014.46
5.6.190.0030.00714.74
5.6.180.0080.00414.78
5.6.170.0070.00714.73
5.6.160.0000.01014.54
5.6.150.0120.00314.68
5.6.140.0100.00714.82
5.6.130.0060.01014.50
5.6.120.0000.01514.46
5.6.110.0110.00714.70
5.6.100.0070.01414.65
5.6.90.0040.01514.68
5.6.80.0070.01514.82
5.6.70.0000.01414.68
5.6.60.0090.00614.41
5.6.50.0000.01214.48
5.6.40.0030.01314.39
5.6.30.0070.00714.52
5.6.20.0060.00614.29
5.6.10.0100.00614.65
5.6.00.0040.01114.34

preferences:
67.21 ms | 401 KiB | 5 Q