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)); var_dump($newTextNode); $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"> b%ELEMENT%a </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.0090.00618.68
8.3.50.0080.00817.00
8.3.40.0120.00419.04
8.3.30.0120.00619.03
8.3.20.0040.00420.61
8.3.10.0050.00324.04
8.3.00.0000.00820.14
8.2.180.0070.01318.91
8.2.170.0100.01322.96
8.2.160.0100.00719.36
8.2.150.0080.00024.18
8.2.140.0030.00624.66
8.2.130.0080.00019.82
8.2.120.0060.00326.35
8.2.110.0070.00320.82
8.2.100.0030.01018.22
8.2.90.0080.00019.77
8.2.80.0060.00318.30
8.2.70.0030.00618.13
8.2.60.0060.00318.30
8.2.50.0030.00618.41
8.2.40.0060.00321.10
8.2.30.0040.00419.73
8.2.20.0080.00018.48
8.2.10.0000.00818.58
8.2.00.0000.00818.44
8.1.280.0060.00925.92
8.1.270.0030.00622.61
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0030.00722.47
8.1.230.0060.00619.47
8.1.220.0080.00017.92
8.1.210.0030.00618.77
8.1.200.0030.00717.85
8.1.190.0030.00617.60
8.1.180.0080.00018.10
8.1.170.0030.00619.02
8.1.160.0000.00819.48
8.1.150.0080.00019.19
8.1.140.0040.00419.94
8.1.130.0030.00619.25
8.1.120.0040.00417.83
8.1.110.0030.00517.79
8.1.100.0000.00817.93
8.1.90.0040.00417.79
8.1.80.0000.00917.78
8.1.70.0000.00817.88
8.1.60.0030.00617.96
8.1.50.0040.00417.84
8.1.40.0140.00017.81
8.1.30.0050.00317.92
8.1.20.0080.00018.06
8.1.10.0040.00417.93
8.1.00.0040.00417.95
8.0.300.0080.00020.32
8.0.290.0000.00917.13
8.0.280.0040.00418.75
8.0.270.0080.00017.61
8.0.260.0040.00417.30
8.0.250.0000.00717.49
8.0.240.0050.00317.45
8.0.230.0000.00817.48
8.0.220.0040.00417.39
8.0.210.0040.00417.27
8.0.200.0040.00417.30
8.0.190.0040.00417.37
8.0.180.0040.00417.43
8.0.170.0000.00817.44
8.0.160.0040.00417.30
8.0.150.0040.00417.21
8.0.140.0060.00317.26
8.0.130.0030.00313.74
8.0.120.0040.00417.39
8.0.110.0000.00817.45
8.0.100.0060.00317.24
8.0.90.0040.00417.39
8.0.80.0050.01317.31
8.0.70.0030.00617.21
8.0.60.0050.00317.47
8.0.50.0000.00817.28
8.0.30.0130.00817.70
8.0.20.0150.01117.71
8.0.10.0040.00417.50
8.0.00.0140.00717.14
7.4.330.0000.00615.55
7.4.320.0070.00016.98
7.4.300.0000.00617.00
7.4.290.0060.00616.88
7.4.280.0040.00417.05
7.4.270.0070.00016.88
7.4.260.0040.00417.02
7.4.250.0000.00916.99
7.4.240.0000.00817.02
7.4.230.0040.00416.95
7.4.220.0130.00616.89
7.4.210.0060.00917.14
7.4.200.0050.00217.07
7.4.160.0110.00716.90
7.4.150.0150.00617.40
7.4.140.0100.01317.86
7.4.130.0150.00717.06
7.4.120.0080.01516.94
7.4.110.0130.01016.95
7.4.100.0130.01016.89
7.4.90.0080.01217.01
7.4.80.0100.01419.39
7.4.70.0110.00816.94
7.4.60.0100.01016.95
7.4.50.0100.01016.72
7.4.40.0070.01617.01
7.4.30.0070.01017.04
7.4.00.0100.00615.19
7.3.330.0060.00013.59
7.3.320.0030.00313.69
7.3.310.0000.00716.80
7.3.300.0000.00716.71
7.3.290.0060.01116.79
7.3.280.0080.01216.76
7.3.270.0120.01217.40
7.3.260.0100.01016.96
7.3.250.0130.00816.89
7.3.240.0090.01217.06
7.3.230.0140.00716.93
7.3.210.0090.00917.06
7.3.200.0070.01416.82
7.3.190.0050.01517.06
7.3.180.0070.01117.08
7.3.170.0070.01116.73
7.3.160.0060.01316.82
7.2.330.0110.00917.21
7.2.320.0130.01316.97
7.2.310.0070.01417.09
7.2.300.0060.01117.20
7.2.290.0080.01117.06
7.2.60.0000.01516.83
7.2.00.0000.01519.17
7.1.200.0030.01016.11
7.1.100.0120.00018.45
7.1.70.0060.01517.34
7.1.60.0350.00935.24
7.1.50.0220.02534.78
7.1.40.0270.02134.53
7.1.30.0260.02034.60
7.1.20.0230.02334.72
7.1.10.0130.01016.70
7.1.00.0160.00316.77
7.0.200.0060.01016.78
7.0.190.0150.00517.12
7.0.180.0080.00816.47
7.0.170.0070.01716.48
7.0.160.0070.02016.57
7.0.150.0090.01316.59
7.0.140.0000.02316.56
7.0.130.0080.00816.64
7.0.120.0030.01516.79
7.0.110.0120.01116.55
7.0.100.0070.01316.54
7.0.90.0080.02016.32
7.0.80.0080.03116.71
7.0.70.0140.01316.52
7.0.60.0130.01216.43
7.0.50.0100.01416.65
7.0.40.0120.00916.61
7.0.30.0120.00616.58
7.0.20.0200.00416.54
7.0.10.0030.02216.71
7.0.00.0100.01416.65

preferences:
41.41 ms | 401 KiB | 5 Q