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); $newNode->parentNode->appendChild($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.0140.00318.68
8.3.50.0060.00917.00
8.3.40.0100.00619.08
8.3.30.0030.01319.12
8.3.20.0030.00520.58
8.3.10.0040.00423.73
8.3.00.0060.00319.81
8.2.180.0200.00317.13
8.2.170.0060.01622.96
8.2.160.0170.00319.48
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0040.00419.75
8.2.120.0090.00026.35
8.2.110.0030.00621.04
8.2.100.0060.00618.25
8.2.90.0090.00019.68
8.2.80.0060.00318.41
8.2.70.0030.00617.87
8.2.60.0030.00718.54
8.2.50.0060.00318.43
8.2.40.0040.00421.15
8.2.30.0050.00521.59
8.2.20.0000.00818.43
8.2.10.0040.00418.43
8.2.00.0040.00418.45
8.1.280.0100.01025.92
8.1.270.0030.00622.69
8.1.260.0080.00026.35
8.1.250.0040.00428.09
8.1.240.0000.01021.14
8.1.230.0030.00919.62
8.1.220.0030.00518.29
8.1.210.0050.00518.77
8.1.200.0070.00317.73
8.1.190.0030.00517.60
8.1.180.0040.00418.10
8.1.170.0050.00318.88
8.1.160.0080.00019.42
8.1.150.0000.00819.24
8.1.140.0050.00319.98
8.1.130.0050.00319.46
8.1.120.0040.00417.88
8.1.110.0030.00517.77
8.1.100.0030.00517.77
8.1.90.0050.00317.70
8.1.80.0080.00017.83
8.1.70.0080.00017.80
8.1.60.0060.00318.04
8.1.50.0040.00417.99
8.1.40.0040.00817.93
8.1.30.0080.00018.05
8.1.20.0040.00418.13
8.1.10.0060.00317.90
8.1.00.0030.00617.95
8.0.300.0050.00320.19
8.0.290.0040.00417.25
8.0.280.0050.00218.80
8.0.270.0040.00417.70
8.0.260.0050.00217.39
8.0.250.0000.00717.36
8.0.240.0080.00017.47
8.0.230.0000.00717.42
8.0.220.0050.00217.28
8.0.210.0080.00017.28
8.0.200.0020.00517.43
8.0.190.0000.00917.29
8.0.180.0040.00417.31
8.0.170.0050.00317.42
8.0.160.0000.00817.31
8.0.150.0040.00417.33
8.0.140.0000.00717.33
8.0.130.0000.00613.75
8.0.120.0040.00417.32
8.0.110.0000.00917.43
8.0.100.0050.00317.39
8.0.90.0030.00517.32
8.0.80.0050.01517.37
8.0.70.0030.00617.24
8.0.60.0080.00017.32
8.0.50.0080.00017.28
8.0.30.0070.01417.57
8.0.20.0120.00917.51
8.0.10.0060.00317.52
8.0.00.0060.01217.41
7.4.330.0000.00615.55
7.4.320.0070.00016.93
7.4.300.0030.00317.04
7.4.290.0070.00416.84
7.4.280.0000.01016.82
7.4.270.0000.00816.90
7.4.260.0050.00316.92
7.4.250.0040.00416.88
7.4.240.0000.00716.92
7.4.230.0070.00017.10
7.4.220.0060.01316.95
7.4.210.0040.01816.80
7.4.200.0080.00016.69
7.4.160.0110.01117.01
7.4.150.0140.00717.40
7.4.140.0090.01417.86
7.4.130.0160.00316.90
7.4.120.0150.01016.92
7.4.110.0120.00916.88
7.4.100.0100.01016.89
7.4.90.0090.01116.99
7.4.80.0110.01119.39
7.4.70.0000.02016.82
7.4.60.0170.00316.91
7.4.50.0030.01016.77
7.4.40.0100.01317.00
7.4.30.0070.01016.89
7.4.00.0040.01215.16
7.3.330.0030.00313.61
7.3.320.0030.00313.56
7.3.310.0040.00416.71
7.3.300.0040.00416.73
7.3.290.0060.01016.78
7.3.280.0130.00816.76
7.3.270.0150.00717.40
7.3.260.0090.01016.76
7.3.250.0120.00916.94
7.3.240.0090.00917.02
7.3.230.0090.00916.96
7.3.210.0090.00916.73
7.3.200.0180.00817.00
7.3.190.0130.00716.85
7.3.180.0090.01216.79
7.3.170.0180.00017.13
7.3.160.0180.00016.76
7.3.10.0080.00616.91
7.3.00.0110.00016.67
7.2.330.0090.01617.16
7.2.320.0120.01216.98
7.2.310.0030.01717.18
7.2.300.0130.00617.19
7.2.290.0100.01117.12
7.2.130.0060.00917.01
7.2.120.0070.00316.89
7.2.110.0120.00516.81
7.2.100.0080.00416.83
7.2.90.0070.00717.14
7.2.80.0030.00917.09
7.2.70.0070.00317.18
7.2.60.0070.00917.12
7.2.50.0060.01217.28
7.2.40.0070.00716.92
7.2.30.0070.00717.31
7.2.20.0060.00617.25
7.2.10.0060.00917.14
7.2.00.0030.01218.35
7.1.250.0070.00715.96
7.1.200.0060.00615.82
7.1.100.0040.00818.36
7.1.70.0130.01017.45
7.1.60.0260.02135.27
7.1.50.0310.02534.95
7.1.40.0360.01234.53
7.1.30.0200.03234.63
7.1.20.0280.01634.66
7.1.10.0100.01816.81
7.1.00.0090.01516.97
7.0.200.0210.01317.14
7.0.190.0130.02016.97
7.0.180.0160.01116.38
7.0.170.0140.00716.46
7.0.160.0110.01116.44
7.0.150.0100.01516.46
7.0.140.0100.01116.68
7.0.130.0130.01316.64
7.0.120.0080.01616.64
7.0.110.0110.01316.66
7.0.100.0130.01516.60
7.0.90.0110.01516.45
7.0.80.0000.02116.52
7.0.70.0060.01416.45
7.0.60.0090.01216.43
7.0.50.0070.01716.53
7.0.40.0110.01216.63
7.0.30.0030.02316.61
7.0.20.0100.01416.63
7.0.10.0210.00416.59
7.0.00.0090.01616.61

preferences:
31.11 ms | 400 KiB | 5 Q