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, $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.0040.01218.68
8.3.50.0060.00916.84
8.3.40.0080.00819.26
8.3.30.0150.00019.17
8.3.20.0080.00020.66
8.3.10.0030.00623.89
8.3.00.0060.00320.19
8.2.180.0090.00618.91
8.2.170.0120.00322.96
8.2.160.0060.00919.46
8.2.150.0080.00024.18
8.2.140.0080.00024.66
8.2.130.0050.00319.82
8.2.120.0080.00026.35
8.2.110.0030.00622.57
8.2.100.0060.00618.03
8.2.90.0030.00619.71
8.2.80.0040.00418.28
8.2.70.0000.00817.97
8.2.60.0040.00418.54
8.2.50.0040.00418.29
8.2.40.0050.00320.92
8.2.30.0040.00419.65
8.2.20.0030.00518.52
8.2.10.0030.00518.51
8.2.00.0040.00418.68
8.1.280.0070.01125.92
8.1.270.0030.00522.59
8.1.260.0040.00426.35
8.1.250.0050.00328.09
8.1.240.0110.00724.17
8.1.230.0000.01219.47
8.1.220.0050.00518.28
8.1.210.0040.00418.77
8.1.200.0030.00617.73
8.1.190.0050.00317.73
8.1.180.0000.00818.10
8.1.170.0000.00920.47
8.1.160.0050.00319.46
8.1.150.0030.00519.45
8.1.140.0030.00519.98
8.1.130.0030.00519.32
8.1.120.0060.00317.87
8.1.110.0080.00017.87
8.1.100.0000.00717.88
8.1.90.0000.00817.82
8.1.80.0040.00417.92
8.1.70.0000.00817.82
8.1.60.0030.00518.03
8.1.50.0030.00717.98
8.1.40.0070.00517.79
8.1.30.0000.00817.97
8.1.20.0000.00818.09
8.1.10.0030.00518.00
8.1.00.0040.00417.80
8.0.300.0000.00820.13
8.0.290.0080.00017.25
8.0.280.0040.00418.86
8.0.270.0000.00717.63
8.0.260.0000.00717.29
8.0.250.0000.00717.48
8.0.240.0040.00417.34
8.0.230.0040.00417.45
8.0.220.0000.00717.41
8.0.210.0050.00317.25
8.0.200.0000.00717.42
8.0.190.0060.00317.46
8.0.180.0000.00817.37
8.0.170.0080.00017.34
8.0.160.0000.00817.31
8.0.150.0040.00417.34
8.0.140.0040.00417.32
8.0.130.0070.00013.77
8.0.120.0000.00817.24
8.0.110.0040.00417.30
8.0.100.0060.00317.45
8.0.90.0040.00417.25
8.0.80.0090.01217.25
8.0.70.0040.00417.46
8.0.60.0080.00017.47
8.0.50.0040.00417.41
8.0.30.0090.01117.65
8.0.20.0110.01017.64
8.0.10.0070.00017.37
8.0.00.0120.00617.00
7.4.330.0050.00015.55
7.4.320.0030.00316.97
7.4.300.0080.00017.02
7.4.290.0070.00317.06
7.4.280.0000.00817.10
7.4.270.0070.00017.02
7.4.260.0040.00416.81
7.4.250.0000.00816.81
7.4.240.0060.00316.97
7.4.230.0070.00016.91
7.4.220.0040.01516.92
7.4.210.0120.00916.86
7.4.200.0040.00417.10
7.4.160.0120.00616.98
7.4.150.0000.01917.40
7.4.140.0110.00817.86
7.4.130.0100.01316.79
7.4.120.0110.01016.90
7.4.110.0060.01217.00
7.4.100.0090.01216.93
7.4.90.0090.00916.88
7.4.80.0070.01919.39
7.4.70.0050.01316.95
7.4.60.0120.00617.02
7.4.50.0100.00716.64
7.4.40.0040.01416.89
7.4.30.0100.00916.93
7.4.00.0000.01115.05
7.3.330.0050.00013.65
7.3.320.0040.00413.69
7.3.310.0000.00716.86
7.3.300.0030.00316.64
7.3.290.0090.00616.81
7.3.280.0080.01016.81
7.3.270.0030.01517.40
7.3.260.0100.00816.91
7.3.250.0100.01016.86
7.3.240.0080.01116.99
7.3.230.0070.01116.83
7.3.210.0130.00316.80
7.3.200.0070.01116.86
7.3.190.0080.01416.87
7.3.180.0100.00616.98
7.3.170.0100.00717.15
7.3.160.0030.01517.08
7.2.330.0090.01017.07
7.2.320.0070.01617.25
7.2.310.0070.01417.22
7.2.300.0080.00816.89
7.2.290.0150.00917.14
7.2.60.0000.01517.01
7.2.00.0110.00719.77
7.1.200.0000.01316.09
7.1.100.0090.00618.62
7.1.70.0070.01117.14
7.1.60.0220.01635.04
7.1.50.0300.01534.85
7.1.40.0290.02234.54
7.1.30.0420.00634.76
7.1.20.0260.01934.69
7.1.10.0100.01516.72
7.1.00.0060.01616.77
7.0.200.0040.01716.76
7.0.190.0140.00716.74
7.0.180.0050.01616.47
7.0.170.0120.01016.47
7.0.160.0060.01516.50
7.0.150.0120.01216.29
7.0.140.0110.01116.45
7.0.130.0030.01516.80
7.0.120.0050.01416.74
7.0.110.0140.00316.47
7.0.100.0090.00916.44
7.0.90.0060.01116.44
7.0.80.0100.00916.60
7.0.70.0100.00716.49
7.0.60.0000.01916.26
7.0.50.0110.00816.46
7.0.40.0030.02016.65
7.0.30.0090.00916.80
7.0.20.0100.00816.54
7.0.10.0060.00916.58
7.0.00.0060.01616.46

preferences:
45.97 ms | 400 KiB | 5 Q