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> </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.0150.00618.93
8.3.50.0060.00916.71
8.3.40.0090.00619.39
8.3.30.0130.00319.12
8.3.20.0090.00020.57
8.3.10.0090.00024.01
8.3.00.0080.00020.06
8.2.180.0180.00018.66
8.2.170.0060.00922.96
8.2.160.0040.01119.64
8.2.150.0000.00924.18
8.2.140.0080.00024.66
8.2.130.0080.00019.56
8.2.120.0050.00326.35
8.2.110.0090.00020.89
8.2.100.0060.00618.41
8.2.90.0060.00319.73
8.2.80.0030.00518.54
8.2.70.0040.00418.00
8.2.60.0090.00018.30
8.2.50.0030.00618.43
8.2.40.0050.00320.92
8.2.30.0040.00419.63
8.2.20.0080.00018.48
8.2.10.0050.00318.52
8.2.00.0000.00818.54
8.1.280.0150.00025.92
8.1.270.0000.00822.57
8.1.260.0030.00526.35
8.1.250.0090.00028.09
8.1.240.0060.00322.41
8.1.230.0000.01119.60
8.1.220.0040.00417.93
8.1.210.0060.00318.77
8.1.200.0030.00717.85
8.1.190.0040.00417.60
8.1.180.0060.00318.10
8.1.170.0000.00819.00
8.1.160.0060.00319.37
8.1.150.0040.00419.19
8.1.140.0030.00520.02
8.1.130.0050.00319.31
8.1.120.0040.00417.79
8.1.110.0050.00317.87
8.1.100.0040.00417.91
8.1.90.0000.00817.89
8.1.80.0000.00817.84
8.1.70.0030.00517.81
8.1.60.0050.00317.98
8.1.50.0000.00817.87
8.1.40.0060.00617.93
8.1.30.0030.00618.06
8.1.20.0080.00017.97
8.1.10.0050.00317.93
8.1.00.0030.00517.98
8.0.300.0000.00820.41
8.0.290.0050.00317.13
8.0.280.0050.00318.77
8.0.270.0080.00017.53
8.0.260.0030.00317.20
8.0.250.0000.00717.45
8.0.240.0050.00317.49
8.0.230.0040.00417.37
8.0.220.0000.00817.28
8.0.210.0040.00417.28
8.0.200.0030.00317.43
8.0.190.0040.00417.37
8.0.180.0080.00017.29
8.0.170.0000.00817.44
8.0.160.0040.00417.28
8.0.150.0000.00717.29
8.0.140.0040.00417.29
8.0.130.0030.00313.80
8.0.120.0040.00417.34
8.0.110.0000.00717.28
8.0.100.0040.00417.43
8.0.90.0040.00417.28
8.0.80.0040.01717.38
8.0.70.0060.00317.35
8.0.60.0000.00717.51
8.0.50.0000.00817.23
8.0.30.0070.01017.37
8.0.20.0090.01217.60
8.0.10.0040.00417.45
8.0.00.0090.00917.47
7.4.330.0000.00515.55
7.4.320.0070.00016.93
7.4.300.0030.00317.04
7.4.290.0030.00917.07
7.4.280.0060.00317.05
7.4.270.0040.00416.94
7.4.260.0000.00717.03
7.4.250.0000.00816.97
7.4.240.0040.00417.04
7.4.230.0050.00317.12
7.4.220.0060.01216.89
7.4.210.0140.00317.14
7.4.200.0000.00816.82
7.4.160.0120.00616.93
7.4.150.0060.01317.40
7.4.140.0110.00917.86
7.4.130.0070.01916.86
7.4.120.0130.00716.96
7.4.110.0140.00716.88
7.4.100.0120.00916.83
7.4.90.0120.00616.73
7.4.80.0130.01619.39
7.4.70.0130.00416.87
7.4.60.0070.01117.17
7.4.50.0080.00616.96
7.4.40.0080.00817.04
7.4.30.0090.00916.94
7.4.00.0070.00714.95
7.3.330.0000.00513.55
7.3.320.0070.00013.56
7.3.310.0000.00716.82
7.3.300.0000.00716.78
7.3.290.0070.01116.77
7.3.280.0090.00916.78
7.3.270.0100.01417.40
7.3.260.0070.01216.84
7.3.250.0110.00816.88
7.3.240.0120.00516.80
7.3.230.0060.01217.07
7.3.210.0120.00616.80
7.3.200.0150.00317.11
7.3.190.0060.01116.77
7.3.180.0060.01016.97
7.3.170.0080.00816.88
7.3.160.0090.01316.85
7.2.330.0140.00717.13
7.2.320.0030.01917.30
7.2.310.0040.01417.22
7.2.300.0000.01517.33
7.2.290.0140.00717.20
7.2.60.0040.01117.19
7.1.200.0060.00315.93
7.1.70.0060.01317.53
7.1.60.0360.01335.23
7.1.50.0320.01735.10
7.1.40.0230.01734.52
7.1.30.0230.01634.59
7.1.20.0260.02234.69
7.1.10.0080.01216.72
7.1.00.0110.00916.76
7.0.200.0140.00816.70
7.0.190.0150.00816.97
7.0.180.0060.01216.61
7.0.170.0080.01116.38
7.0.160.0060.01616.47
7.0.150.0070.01116.41
7.0.140.0090.00916.35
7.0.130.0100.01316.71
7.0.120.0110.01116.52
7.0.110.0120.00616.55
7.0.100.0030.01316.44
7.0.90.0030.01516.63
7.0.80.0030.01716.52
7.0.70.0120.01216.47
7.0.60.0120.01116.37
7.0.50.0040.01816.50
7.0.40.0130.01216.62
7.0.30.0120.00916.64
7.0.20.0060.01116.56
7.0.10.0090.00916.73
7.0.00.0040.01816.46

preferences:
41.52 ms | 400 KiB | 5 Q