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->parentNode->insertBefore($newTextNode->cloneNode(true), $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.0160.00018.68
8.3.50.0100.01016.76
8.3.40.0110.00419.34
8.3.30.0040.01119.34
8.3.20.0050.00320.54
8.3.10.0040.00422.00
8.3.00.0080.00020.14
8.2.180.0160.00618.75
8.2.170.0040.01122.96
8.2.160.0160.00619.76
8.2.150.0090.00024.18
8.2.140.0040.00424.66
8.2.130.0030.00519.63
8.2.120.0040.00426.35
8.2.110.0090.00022.64
8.2.100.0090.00318.41
8.2.90.0030.00519.57
8.2.80.0060.00318.00
8.2.70.0000.00818.00
8.2.60.0090.00018.43
8.2.50.0040.00418.41
8.2.40.0050.00320.97
8.2.30.0000.00821.42
8.2.20.0080.00018.44
8.2.10.0040.00418.55
8.2.00.0040.00418.48
8.1.280.0060.01025.92
8.1.270.0030.00622.56
8.1.260.0040.00426.35
8.1.250.0090.00028.09
8.1.240.0070.00323.11
8.1.230.0040.00818.21
8.1.220.0050.00318.30
8.1.210.0040.00418.77
8.1.200.0070.00317.98
8.1.190.0040.00417.75
8.1.180.0080.00018.10
8.1.170.0040.00418.90
8.1.160.0030.00519.43
8.1.150.0030.00519.41
8.1.140.0040.00419.95
8.1.130.0000.00719.29
8.1.120.0000.00817.84
8.1.110.0050.00317.78
8.1.100.0000.00817.94
8.1.90.0050.00317.85
8.1.80.0040.00417.75
8.1.70.0040.00417.84
8.1.60.0050.00318.04
8.1.50.0080.00018.00
8.1.40.0070.00717.82
8.1.30.0080.00018.13
8.1.20.0030.00518.12
8.1.10.0040.00418.13
8.1.00.0030.00618.05
8.0.300.0040.00418.77
8.0.290.0080.00017.13
8.0.280.0030.00618.83
8.0.270.0040.00417.63
8.0.260.0020.00517.27
8.0.250.0040.00417.48
8.0.240.0000.00717.37
8.0.230.0040.00417.34
8.0.220.0040.00417.43
8.0.210.0070.00017.33
8.0.200.0000.00717.36
8.0.190.0030.00617.46
8.0.180.0040.00417.34
8.0.170.0050.00217.43
8.0.160.0060.00317.51
8.0.150.0080.00017.22
8.0.140.0030.00717.29
8.0.130.0000.00613.78
8.0.120.0030.00617.38
8.0.110.0080.00017.19
8.0.100.0040.00417.19
8.0.90.0000.00917.41
8.0.80.0100.00717.39
8.0.70.0030.00617.46
8.0.60.0000.00817.47
8.0.50.0060.00317.25
8.0.30.0060.01117.50
8.0.20.0140.01317.65
8.0.10.0040.00417.32
8.0.00.0120.00617.26
7.4.330.0000.00615.55
7.4.320.0030.00316.98
7.4.300.0000.00617.02
7.4.290.0100.00016.97
7.4.280.0080.00017.08
7.4.270.0030.00316.97
7.4.260.0000.00717.05
7.4.250.0080.00016.98
7.4.240.0000.00717.00
7.4.230.0040.00417.17
7.4.220.0100.01016.99
7.4.210.0090.00617.18
7.4.200.0000.00716.86
7.4.160.0110.00617.04
7.4.150.0140.00617.40
7.4.140.0110.00817.86
7.4.130.0110.01516.91
7.4.120.0110.00916.94
7.4.110.0070.01016.78
7.4.100.0070.01516.92
7.4.90.0040.01517.05
7.4.80.0100.00719.39
7.4.70.0060.01117.02
7.4.60.0070.01016.83
7.4.50.0070.00716.84
7.4.40.0100.00716.96
7.4.30.0150.00517.00
7.4.10.0060.01315.13
7.4.00.0070.01015.16
7.3.330.0000.00513.50
7.3.320.0070.00013.69
7.3.310.0020.00516.82
7.3.300.0030.00516.69
7.3.290.0110.00716.81
7.3.280.0070.01116.80
7.3.270.0090.00917.40
7.3.260.0120.00716.94
7.3.250.0140.00716.93
7.3.240.0090.01116.91
7.3.230.0100.00716.69
7.3.210.0110.00716.74
7.3.200.0120.00617.02
7.3.190.0060.01817.09
7.3.180.0100.00616.82
7.3.170.0130.00316.94
7.3.160.0000.01716.92
7.3.130.0030.01515.33
7.3.120.0110.00615.05
7.3.110.0090.00915.03
7.3.100.0030.01015.08
7.3.90.0070.00715.21
7.3.80.0060.00615.23
7.3.70.0070.00715.12
7.3.60.0060.00915.11
7.3.50.0060.00915.26
7.3.40.0000.01715.17
7.3.30.0040.01115.11
7.3.20.0090.00017.02
7.3.10.0030.01016.84
7.3.00.0050.00816.78
7.2.330.0040.01416.98
7.2.320.0150.00917.25
7.2.310.0060.01117.04
7.2.300.0100.00717.05
7.2.290.0080.01117.12
7.2.260.0000.01815.23
7.2.250.0100.01015.14
7.2.240.0080.00615.43
7.2.230.0090.00915.25
7.2.220.0070.00715.34
7.2.210.0030.01015.53
7.2.200.0030.00915.48
7.2.190.0000.01015.11
7.2.180.0080.00815.47
7.2.170.0000.00915.40
7.2.160.0060.01015.34
7.2.150.0030.00617.09
7.2.140.0070.00717.10
7.2.130.0010.01217.13
7.2.120.0070.00817.13
7.2.110.0050.00717.12
7.2.100.0060.00716.98
7.2.90.0050.00917.19
7.2.80.0050.01117.27
7.2.70.0050.00917.20
7.2.60.0030.00917.13
7.2.50.0040.00617.29
7.2.40.0020.01117.26
7.2.30.0030.01217.02
7.2.20.0110.00417.12
7.2.10.0060.00517.15
7.2.00.0030.01118.09
7.1.330.0110.00415.80
7.1.320.0030.00716.04
7.1.310.0040.01116.09
7.1.300.0000.00916.02
7.1.290.0070.00715.82
7.1.280.0060.00616.07
7.1.270.0030.01315.98
7.1.260.0000.01115.96
7.1.250.0050.00815.94
7.1.240.0130.00316.11
7.1.230.0080.00816.20
7.1.220.0030.00715.88
7.1.210.0040.00815.78
7.1.200.0060.00716.03
7.1.190.0090.00315.91
7.1.180.0060.00615.95
7.1.170.0000.01415.74
7.1.160.0030.00816.20
7.1.150.0000.01416.11
7.1.140.0040.00416.11
7.1.130.0000.01615.85
7.1.120.0100.00616.05
7.1.110.0080.00316.05
7.1.100.0100.00817.03
7.1.90.0060.00915.84
7.1.80.0060.00615.90
7.1.70.0350.00816.68
7.1.60.0390.01024.75
7.1.50.0250.01224.40
7.1.40.0430.00824.41
7.1.30.0180.01525.18
7.1.20.0390.01224.42
7.1.10.0070.01216.36
7.1.00.0060.01316.30
7.0.330.0060.00615.61
7.0.320.0060.00615.30
7.0.310.0000.01015.60
7.0.300.0030.00915.47
7.0.290.0070.00715.31
7.0.280.0070.00715.73
7.0.270.0100.00315.78
7.0.260.0030.00815.71
7.0.250.0000.01115.73
7.0.240.0060.00615.55
7.0.230.0000.00915.75
7.0.220.0000.01315.59
7.0.210.0100.00315.55
7.0.200.0230.00915.48
7.0.190.0200.00716.36
7.0.180.0030.01616.08
7.0.170.0320.01215.19
7.0.160.0100.01116.04
7.0.150.0220.01315.07
7.0.140.0240.01215.20
7.0.130.0270.01215.34
7.0.120.0220.00415.22
7.0.110.0200.01015.19
7.0.100.0240.01215.12
7.0.90.0260.00715.27
7.0.80.0230.00715.17
7.0.70.0160.01015.14
7.0.60.0200.01414.93
7.0.50.0180.00815.19
7.0.40.0200.00915.15
7.0.30.0240.01514.25
7.0.20.0050.01314.88
7.0.10.0050.01115.11
7.0.00.0030.01515.11
5.6.400.0030.01214.69
5.6.390.0030.01015.04
5.6.380.0100.00314.70
5.6.370.0060.00314.73
5.6.360.0070.00714.42
5.6.350.0090.00614.66
5.6.340.0000.01114.91
5.6.330.0060.00914.63
5.6.320.0050.00814.55
5.6.310.0100.00714.82
5.6.300.0030.00914.89
5.6.290.0030.00914.54
5.6.280.0030.01214.50
5.6.270.0000.00914.45
5.6.260.0030.01014.77
5.6.250.0060.00914.66
5.6.240.0070.00714.57
5.6.230.0040.00714.86
5.6.220.0070.00714.60
5.6.210.0120.00314.74
5.6.200.0090.00314.70
5.6.190.0030.01014.64
5.6.180.0000.01514.63
5.6.170.0060.00914.42
5.6.160.0040.01114.67
5.6.150.0070.00714.69
5.6.140.0060.00914.80
5.6.130.0040.00814.55
5.6.120.0040.01114.68
5.6.110.0130.00014.60
5.6.100.0110.00414.82
5.6.90.0000.01514.64
5.6.80.0040.01414.71
5.6.70.0060.00314.78
5.6.60.0000.01114.56
5.6.50.0040.00714.60
5.6.40.0000.01414.55
5.6.30.0030.01014.55
5.6.20.0070.01014.59
5.6.10.0030.01014.58
5.6.00.0070.00714.74

preferences:
41.76 ms | 401 KiB | 5 Q