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)); $textNode->nextSibling->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.60.0070.00718.68
8.3.50.0150.00016.78
8.3.40.0000.01519.52
8.3.30.0090.00618.94
8.3.20.0040.00420.54
8.3.10.0040.00423.95
8.3.00.0080.00020.06
8.2.180.0090.01218.80
8.2.170.0120.00322.96
8.2.160.0110.00419.59
8.2.150.0080.00024.18
8.2.140.0030.00524.66
8.2.130.0050.00319.77
8.2.120.0040.00426.35
8.2.110.0000.00921.03
8.2.100.0040.00818.41
8.2.90.0030.00519.58
8.2.80.0000.00818.37
8.2.70.0040.00418.13
8.2.60.0060.00318.66
8.2.50.0090.00018.55
8.2.40.0060.00320.97
8.2.30.0060.00621.44
8.2.20.0000.00818.45
8.2.10.0050.00318.41
8.2.00.0000.00818.58
8.1.280.0110.00425.92
8.1.270.0080.00024.22
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0090.00022.50
8.1.230.0090.00319.59
8.1.220.0040.00418.15
8.1.210.0030.00618.77
8.1.200.0060.00317.73
8.1.190.0060.00317.72
8.1.180.0060.00318.10
8.1.170.0030.00518.96
8.1.160.0050.00319.37
8.1.150.0040.00419.28
8.1.140.0000.00819.94
8.1.130.0040.00419.34
8.1.120.0080.00017.94
8.1.110.0000.00817.84
8.1.100.0000.00817.92
8.1.90.0050.00317.86
8.1.80.0000.00817.77
8.1.70.0050.00217.89
8.1.60.0060.00318.02
8.1.50.0000.00817.98
8.1.40.0060.00617.85
8.1.30.0000.00818.00
8.1.20.0000.00818.09
8.1.10.0050.00318.05
8.1.00.0030.00617.91
8.0.300.0040.00420.16
8.0.290.0050.00317.23
8.0.280.0040.00418.76
8.0.270.0070.00017.57
8.0.260.0040.00417.27
8.0.250.0000.00717.38
8.0.240.0040.00417.31
8.0.230.0070.00017.44
8.0.220.0040.00417.41
8.0.210.0040.00417.27
8.0.200.0000.00717.46
8.0.190.0000.00817.34
8.0.180.0080.00017.31
8.0.170.0080.00017.40
8.0.160.0040.00417.32
8.0.150.0030.00517.27
8.0.140.0040.00417.38
8.0.130.0060.00013.84
8.0.120.0040.00417.39
8.0.110.0040.00417.47
8.0.100.0040.00417.22
8.0.90.0060.00317.42
8.0.80.0130.00317.45
8.0.70.0040.00417.17
8.0.60.0040.00417.51
8.0.50.0040.00417.22
8.0.30.0110.00817.58
8.0.20.0150.00717.56
8.0.10.0060.00317.39
8.0.00.0070.01017.09
7.4.330.0000.00515.55
7.4.320.0030.00316.95
7.4.300.0030.00317.00
7.4.290.0080.00417.02
7.4.280.0050.00316.86
7.4.270.0000.00916.97
7.4.260.0040.00416.96
7.4.250.0020.00516.96
7.4.240.0000.00717.04
7.4.230.0040.00416.95
7.4.220.0160.00917.09
7.4.210.0110.01116.96
7.4.200.0080.00017.07
7.4.160.0080.00916.85
7.4.150.0130.00617.40
7.4.140.0100.01017.86
7.4.130.0160.00916.75
7.4.120.0150.00516.97
7.4.110.0090.00916.78
7.4.100.0140.00416.87
7.4.90.0090.00916.72
7.4.80.0080.01519.39
7.4.70.0050.01117.10
7.4.60.0090.00616.82
7.4.50.0070.01016.78
7.4.40.0120.00616.98
7.4.30.0090.00916.95
7.4.00.0120.00315.45
7.3.330.0000.00613.73
7.3.320.0030.00313.77
7.3.310.0000.00716.73
7.3.300.0030.00516.70
7.3.290.0080.00816.78
7.3.280.0100.00816.78
7.3.270.0090.01217.40
7.3.260.0070.01016.91
7.3.250.0100.00916.94
7.3.240.0120.01116.81
7.3.230.0140.00617.04
7.3.210.0070.01116.92
7.3.200.0070.01416.80
7.3.190.0150.00316.80
7.3.180.0030.01316.90
7.3.170.0090.00616.97
7.3.160.0100.00716.91
7.2.330.0110.00817.14
7.2.320.0070.01017.25
7.2.310.0120.00617.07
7.2.300.0090.00616.86
7.2.290.0060.01217.26
7.2.60.0030.00917.18
7.2.00.0070.01019.50
7.1.200.0060.01216.03
7.1.100.0040.00418.18
7.1.70.0040.01417.54
7.1.60.0250.01135.27
7.1.50.0490.01233.27
7.1.40.0340.01634.52
7.1.30.0410.02032.89
7.1.20.0500.02134.64
7.1.10.0430.00614.83
7.1.00.0860.01715.21
7.0.200.0250.00915.36
7.0.190.0360.01915.23
7.0.180.0400.01316.47
7.0.170.0330.01014.66
7.0.160.0300.01814.65
7.0.150.0370.01414.84
7.0.140.0160.00716.47
7.0.130.0090.01216.70
7.0.120.0400.01316.66
7.0.110.0380.01314.72
7.0.100.0050.01516.45
7.0.90.0040.01916.64
7.0.80.0400.01016.51
7.0.70.0080.01416.31
7.0.60.0460.01214.36
7.0.50.0350.01214.81
7.0.40.0460.01816.60
7.0.30.0490.01416.69
7.0.20.0430.00914.86
7.0.10.0420.01516.55
7.0.00.0500.01414.82

preferences:
44.43 ms | 400 KiB | 5 Q