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'); 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.70.0090.00918.68
8.3.60.0070.00718.45
8.3.50.0000.01516.89
8.3.40.0150.00019.48
8.3.30.0120.00319.07
8.3.20.0080.00020.66
8.3.10.0050.00324.03
8.3.00.0030.00620.04
8.2.180.0060.01218.79
8.2.170.0110.00422.96
8.2.160.0090.01219.54
8.2.150.0000.00824.18
8.2.140.0080.00024.66
8.2.130.0030.00526.16
8.2.120.0000.00921.48
8.2.110.0040.00420.89
8.2.100.0090.00318.20
8.2.90.0030.00619.77
8.2.80.0000.00818.29
8.2.70.0050.00318.00
8.2.60.0030.00618.30
8.2.50.0000.00818.41
8.2.40.0040.00420.79
8.2.30.0040.00421.54
8.2.20.0030.00618.59
8.2.10.0080.00018.48
8.2.00.0030.00518.47
8.1.280.0070.01025.92
8.1.270.0040.00424.34
8.1.260.0030.00526.35
8.1.250.0030.00528.09
8.1.240.0050.00522.36
8.1.230.0040.00819.60
8.1.220.0080.00018.16
8.1.210.0030.00618.77
8.1.200.0060.00317.85
8.1.190.0060.00317.50
8.1.180.0040.00418.10
8.1.170.0000.00819.07
8.1.160.0040.00419.56
8.1.150.0020.00519.32
8.1.140.0060.00320.03
8.1.130.0000.00719.16
8.1.120.0040.00417.83
8.1.110.0000.00717.82
8.1.100.0000.00817.78
8.1.90.0060.00317.92
8.1.80.0040.00417.79
8.1.70.0040.00417.93
8.1.60.0060.00318.04
8.1.50.0000.00817.96
8.1.40.0030.00817.86
8.1.30.0000.00817.96
8.1.20.0080.00018.08
8.1.10.0040.00418.00
8.1.00.0040.00417.83
8.0.300.0060.00320.13
8.0.290.0000.00817.13
8.0.280.0000.00718.76
8.0.270.0020.00517.66
8.0.260.0000.00817.28
8.0.250.0050.00217.44
8.0.240.0040.00417.43
8.0.230.0040.00417.45
8.0.220.0030.00317.42
8.0.210.0000.00817.38
8.0.200.0030.00317.35
8.0.190.0060.00317.48
8.0.180.0050.00317.40
8.0.170.0040.00417.32
8.0.160.0040.00417.34
8.0.150.0000.00717.35
8.0.140.0040.00417.31
8.0.130.0020.00513.80
8.0.120.0000.00817.37
8.0.110.0060.00317.41
8.0.100.0030.00517.31
8.0.90.0060.00317.27
8.0.80.0100.01017.37
8.0.70.0050.00317.38
8.0.60.0080.00017.32
8.0.50.0040.00417.44
8.0.30.0070.01117.49
8.0.20.0140.00717.50
8.0.10.0080.00017.41
8.0.00.0120.00617.21
7.4.330.0030.00315.55
7.4.320.0000.00716.91
7.4.300.0000.00616.94
7.4.290.0060.00816.98
7.4.280.0070.00316.86
7.4.270.0070.00017.03
7.4.260.0020.00516.92
7.4.250.0040.00416.88
7.4.240.0050.00216.99
7.4.230.0000.00716.82
7.4.220.0170.00616.80
7.4.210.0040.01817.13
7.4.200.0040.00416.95
7.4.160.0110.00516.73
7.4.150.0080.01117.40
7.4.140.0100.00817.86
7.4.130.0100.01316.92
7.4.120.0120.00816.89
7.4.110.0070.01016.98
7.4.100.0120.00617.01
7.4.90.0160.00316.96
7.4.80.0120.01219.39
7.4.70.0120.00816.91
7.4.60.0060.00916.88
7.4.50.0140.00316.88
7.4.40.0100.00716.90
7.4.30.0100.00716.97
7.4.10.0100.00615.28
7.4.00.0130.00315.16
7.3.330.0050.00013.78
7.3.320.0050.00013.61
7.3.310.0000.00716.76
7.3.300.0030.00316.76
7.3.290.0060.00916.77
7.3.280.0150.00816.76
7.3.270.0040.01417.40
7.3.260.0110.00716.91
7.3.250.0110.00816.92
7.3.240.0080.01116.69
7.3.230.0140.00316.79
7.3.210.0080.00816.89
7.3.200.0070.01116.91
7.3.190.0080.00816.98
7.3.180.0100.00717.13
7.3.170.0060.01216.99
7.3.160.0060.01016.89
7.3.130.0100.01015.21
7.3.120.0040.01415.07
7.3.110.0030.01715.06
7.3.100.0060.00615.09
7.3.90.0030.01315.17
7.3.80.0110.00314.80
7.3.70.0080.00315.16
7.3.60.0100.00415.10
7.3.50.0000.01115.23
7.3.40.0140.00415.11
7.3.30.0060.00915.07
7.3.20.0120.00316.69
7.3.10.0040.00816.80
7.3.00.0060.00916.65
7.2.330.0080.01117.16
7.2.320.0040.01316.85
7.2.310.0070.01117.05
7.2.300.0070.01017.05
7.2.290.0120.00817.07
7.2.260.0090.00915.34
7.2.250.0110.00715.27
7.2.240.0080.00815.27
7.2.230.0110.00715.30
7.2.220.0060.00615.14
7.2.210.0070.00415.23
7.2.200.0030.00715.29
7.2.190.0070.00315.02
7.2.180.0090.00615.39
7.2.170.0070.00415.07
7.2.160.0060.00715.12
7.2.150.0070.00716.82
7.2.140.0060.00317.10
7.2.130.0040.00817.09
7.2.120.0080.00817.09
7.2.110.0090.00717.34
7.2.100.0070.00717.15
7.2.90.0080.00417.25
7.2.80.0060.00917.11
7.2.70.0040.01417.02
7.2.60.0030.01516.97
7.2.50.0040.01117.07
7.2.40.0060.00917.36
7.2.30.0050.00517.21
7.2.20.0060.00617.09
7.2.10.0060.00916.91
7.2.00.0060.00818.42
7.1.330.0000.00815.74
7.1.320.0080.00416.20
7.1.310.0030.00816.27
7.1.300.0050.00715.94
7.1.290.0090.00615.96
7.1.280.0060.00815.98
7.1.270.0000.00915.92
7.1.260.0030.01315.78
7.1.250.0030.01015.98
7.1.240.0100.00316.11
7.1.230.0120.00415.89
7.1.220.0070.00316.00
7.1.210.0000.01415.79
7.1.200.0030.00715.94
7.1.190.0080.00815.75
7.1.180.0030.01616.20
7.1.170.0130.00315.84
7.1.160.0030.00715.76
7.1.150.0040.01415.93
7.1.140.0040.01115.98
7.1.130.0120.00316.07
7.1.120.0110.00416.21
7.1.110.0060.00616.01
7.1.100.0070.00717.18
7.1.90.0030.01015.84
7.1.80.0000.01615.84
7.1.70.0230.01115.67
7.1.60.0400.01124.65
7.1.50.0310.01124.71
7.1.40.0270.01125.12
7.1.30.0140.01025.35
7.1.20.0180.01025.28
7.1.10.0060.01016.48
7.1.00.0030.01516.38
7.0.330.0080.00415.40
7.0.320.0000.01615.65
7.0.310.0030.00715.76
7.0.300.0130.00015.45
7.0.290.0090.00315.47
7.0.280.0000.01415.61
7.0.270.0100.00715.38
7.0.260.0070.00715.65
7.0.250.0030.01015.75
7.0.240.0060.00915.73
7.0.230.0000.01415.63
7.0.220.0100.00315.75
7.0.210.0120.00315.66
7.0.200.0210.01515.54
7.0.190.0310.00815.40
7.0.180.0200.01015.27
7.0.170.0070.00916.08
7.0.160.0030.01116.09
7.0.150.0060.01216.08
7.0.140.0050.01216.07
7.0.130.0080.01216.24
7.0.120.0090.00816.18
7.0.110.0000.01716.04
7.0.100.0130.00716.04
7.0.90.0270.00815.27
7.0.80.0220.01215.25
7.0.70.0280.00715.01
7.0.60.0150.01015.17
7.0.50.0240.00915.20
7.0.40.0240.00714.23
7.0.30.0270.00914.21
7.0.20.0200.00514.27
7.0.10.0180.01114.26
7.0.00.0320.00614.25
5.6.400.0070.00714.59
5.6.390.0040.01514.73
5.6.380.0000.01814.93
5.6.370.0130.00314.42
5.6.360.0090.00614.63
5.6.350.0150.00314.68
5.6.340.0030.01314.69
5.6.330.0090.00914.98
5.6.320.0030.00614.78
5.6.310.0110.00415.00
5.6.300.0030.01714.83
5.6.290.0070.01414.74
5.6.280.0030.01614.74
5.6.270.0060.00314.79
5.6.260.0030.01014.55
5.6.250.0090.00614.78
5.6.240.0070.01014.60
5.6.230.0030.01214.49
5.6.220.0070.00714.53
5.6.210.0000.01414.73
5.6.200.0060.00914.39
5.6.190.0040.01114.55
5.6.180.0040.01114.73
5.6.170.0060.01214.96
5.6.160.0090.00914.59
5.6.150.0080.00615.07
5.6.140.0070.00414.66
5.6.130.0150.00014.54
5.6.120.0060.00614.48
5.6.110.0070.00714.66
5.6.100.0000.01815.04
5.6.90.0080.00814.54
5.6.80.0080.00814.70
5.6.70.0100.00314.44
5.6.60.0030.01014.54
5.6.50.0030.00814.53
5.6.40.0000.01114.44
5.6.30.0030.01314.67
5.6.20.0000.01314.62
5.6.10.0110.00414.91
5.6.00.0050.00514.38

preferences:
55.63 ms | 400 KiB | 5 Q