3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = <<<HTML foo <a href='http://test.com'>fóo</a> lórem bár ipsum bar food foo bark. <a>bar</a> not á test HTML; $lookup = [ 'foo' => 'h3', 'bar' => 'h2' ]; libxml_use_internal_errors(true); $dom = new DOMDocument(); $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $xpath = new DOMXPath($dom); $regexNeedles = []; foreach ($lookup as $word => $tagName) { $regexNeedles[] = preg_quote($word, '~'); } $pattern = '~\b(' . implode('|', $regexNeedles) . ')\b~iu' ; foreach($xpath->query('//*[not(self::a)]/text()') as $textNode) { $newNodes = []; $hasReplacement = false; foreach (preg_split($pattern, $textNode->nodeValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $fragment) { $fragmentLower = strtolower($fragment); if (isset($lookup[$fragmentLower])) { $hasReplacement = true; $a = $dom->createElement($lookup[$fragmentLower]); $a->nodeValue = $fragment; $newNodes[] = $a; } else { $newNodes[] = $dom->createTextNode($fragment); } } if ($hasReplacement) { $newFragment = $dom->createDocumentFragment(); foreach ($newNodes as $newNode) { $newFragment->appendChild($newNode); } $textNode->parentNode->replaceChild($newFragment, $textNode); } } echo substr(trim(utf8_decode($dom->saveHTML($dom->documentElement))), 3, -4);

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.0110.01119.18
8.3.50.0150.00717.01
8.3.40.0180.00422.23
8.3.30.0070.01019.36
8.3.20.0000.00824.18
8.3.10.0080.00024.66
8.3.00.0060.00326.16
8.2.180.0090.00625.92
8.2.170.0090.00619.86
8.2.160.0060.01022.96
8.2.150.0080.00025.66
8.2.140.0040.00424.66
8.2.130.0080.00026.16
8.2.120.0040.00421.38
8.2.110.0060.00320.89
8.2.100.0080.00418.53
8.2.90.0030.00618.38
8.2.80.0040.00419.57
8.2.70.0040.00418.30
8.2.60.0040.00418.72
8.2.50.0080.00018.25
8.2.40.0000.00819.84
8.2.30.0080.00019.82
8.2.20.0030.00618.58
8.2.10.0040.00419.77
8.2.00.0050.00319.82
8.1.280.0000.01625.92
8.1.270.0060.00324.14
8.1.260.0050.00326.35
8.1.250.0000.00828.09
8.1.240.0060.00322.57
8.1.230.0050.00522.66
8.1.220.0050.00319.31
8.1.210.0030.00518.77
8.1.200.0090.00018.10
8.1.190.0060.00317.98
8.1.180.0030.00618.10
8.1.170.0050.00319.15
8.1.160.0030.00519.52
8.1.150.0040.00419.34
8.1.140.0050.00319.27
8.1.130.0030.00322.40
8.1.120.0000.00817.93
8.1.110.0050.00317.99
8.1.100.0000.00817.95
8.1.90.0000.00817.96
8.1.80.0040.00417.94
8.1.70.0000.00717.96
8.1.60.0030.00518.12
8.1.50.0040.00718.09
8.1.40.0050.00518.02
8.1.30.0090.00318.16
8.1.20.0060.00318.23
8.1.10.0080.00018.16
8.1.00.0000.00817.93
8.0.300.0040.00420.42
8.0.290.0040.00417.50
8.0.280.0000.00818.86
8.0.270.0070.00017.39
8.0.260.0000.00718.87
8.0.250.0040.00417.48
8.0.240.0000.00717.48
8.0.230.0000.00717.52
8.0.220.0000.00817.61
8.0.210.0030.00617.50
8.0.200.0030.00317.54
8.0.190.0040.00417.55
8.0.180.0080.00017.43
8.0.170.0000.00817.46
8.0.160.0030.00617.55
8.0.150.0070.00017.31
8.0.140.0000.00817.54
8.0.130.0000.00613.93
8.0.120.0050.00517.61
8.0.110.0000.00817.42
8.0.100.0040.00417.65
8.0.90.0070.00017.55
8.0.80.0110.00617.58
8.0.70.0040.00417.66
8.0.60.0080.00017.37
8.0.50.0000.00817.50
8.0.30.0140.00417.89
8.0.20.0050.01417.74
8.0.10.0000.00817.67
8.0.00.0100.01017.33
7.4.330.0050.00015.55
7.4.320.0030.00317.14
7.4.300.0030.00317.07
7.4.290.0030.00317.15
7.4.280.0040.00417.13
7.4.270.0080.00017.16
7.4.260.0000.00817.20
7.4.250.0050.00217.06
7.4.240.0000.00717.14
7.4.230.0040.00417.33
7.4.220.0030.00417.10
7.4.210.0080.00717.17
7.4.200.0040.00317.29
7.4.130.0140.00717.24
7.4.120.0160.00917.01
7.4.110.0110.00817.19
7.4.100.0090.00917.20
7.4.90.0090.00917.23
7.4.80.0160.00617.08
7.4.70.0170.00417.21
7.4.60.0090.01417.00
7.4.50.0100.01417.21
7.4.40.0160.01017.11
7.4.30.0100.01417.31
7.4.20.0180.00816.93
7.4.10.0040.01917.20
7.4.00.0120.01117.26
7.3.330.0070.00016.76
7.3.320.0030.00313.88
7.3.310.0000.00716.86
7.3.300.0000.00716.89
7.3.290.0070.00816.92
7.3.260.0180.00716.90
7.3.230.0140.00416.92
7.3.220.0090.00917.03
7.3.210.0070.01017.00
7.3.200.0090.00917.04
7.3.190.0190.00617.05
7.3.180.0040.01916.93
7.3.170.0180.00717.17
7.3.160.0100.00916.83
7.3.150.0110.01316.85
7.3.140.0070.02017.25
7.3.130.0130.01016.88
7.3.120.0130.01017.17
7.3.110.0160.01016.97
7.3.100.0120.01217.14
7.3.90.0060.01516.95
7.3.80.0100.01416.90
7.3.70.0130.00616.93
7.3.60.0140.01017.02
7.3.50.0130.01016.87
7.3.40.0090.00917.02
7.3.30.0200.00317.04
7.3.20.0110.01016.89
7.3.10.0060.01317.06
7.3.00.0110.00817.03
7.2.340.0030.01617.06
7.2.330.0080.01317.16
7.2.320.0090.01117.25
7.2.310.0100.01317.09
7.2.300.0110.01417.36
7.2.290.0180.00917.19
7.2.280.0100.01517.32
7.2.270.0160.00917.18
7.2.260.0160.00917.10
7.2.250.0170.00617.21
7.2.240.0170.00817.23
7.2.230.0160.00617.36
7.2.220.0130.01317.39
7.2.210.0100.01017.13
7.2.200.0150.00617.02
7.2.190.0170.00717.05
7.2.180.0190.00617.34
7.2.170.0100.01017.01
7.2.160.0070.01117.09
7.2.150.0140.00617.41
7.2.140.0130.01017.30
7.2.130.0200.01017.16
7.2.120.0060.01217.21
7.2.110.0120.00617.30
7.2.100.0140.00717.19
7.2.90.0180.00617.20
7.2.80.0140.00617.22
7.2.70.0130.01317.23
7.2.60.0120.00917.30
7.2.50.0160.00817.09
7.2.40.0080.01217.38
7.2.30.0120.00917.32
7.2.20.0080.01517.14
7.2.10.0170.00817.40
7.2.00.0140.01017.05

preferences:
63.02 ms | 401 KiB | 5 Q