3v4l.org

run code in 300+ PHP versions simultaneously
<?php $html = 'this is just some example text.'; $terms = array( 'example'=>'explanation about example' ); // sort by reverse order of key size // (to be sure that the longest string always wins instead of the first in the pattern) uksort($terms, function ($a, $b) { $diff = mb_strlen($b) - mb_strlen($a); return ($diff) ? $diff : strcmp($a, $b); }); // build the pattern inside a capture group (to have delimiters in the results with the PREG_SPLIT_DELIM_CAPTURE option) $pattern = '~\b(' . implode('|', array_map(function($i) { return preg_quote($i, '~'); }, array_keys($terms))) . ')\b~i'; // prevent eventual html errors to be displayed $libxmlInternalErrors = libxml_use_internal_errors(true); // determine if the html string have a root html element already, if not add a fake root. $dom = new DOMDocument; $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $fakeRootElement = false; if ( $dom->documentElement->nodeName !== 'html' ) { $dom->loadHTML("<div>$html</div>", LIBXML_HTML_NODEFDTD | LIBXML_HTML_NOIMPLIED); $fakeRootElement = true; } libxml_use_internal_errors($libxmlInternalErrors); // find all text nodes (not already included in a link or between other unwanted tags) $xp = new DOMXPath($dom); $textNodes = $xp->query('//text()[not(ancestor::a)][not(ancestor::style)][not(ancestor::script)]'); // replacement foreach ($textNodes as $textNode) { $parts = preg_split($pattern, $textNode->nodeValue, -1, PREG_SPLIT_DELIM_CAPTURE); $fragment = $dom->createDocumentFragment(); foreach ($parts as $k=>$part) { if ($k&1) { $anchor = $dom->createElement('a', $part); $anchor->setAttribute('class', 'text-info'); $anchor->setAttribute('data-toggle', 'tooltip'); $anchor->setAttribute('data-original-title', $terms[strtolower($part)]); $fragment->appendChild($anchor); } else { $fragment->appendChild($dom->createTextNode($part)); } } $textNode->parentNode->replaceChild($fragment, $textNode); } // building of the result string $result = ''; if ( $fakeRootElement ) { foreach ($dom->documentElement->childNodes as $childNode) { $result .= $dom->saveHTML($childNode); } } else { $result = $dom->saveHTML(); } echo $result;

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.5.10.0130.00517.21
8.5.00.0120.00920.48
8.4.150.0010.00214.05
8.4.140.0150.00618.43
8.4.130.0110.00718.97
8.4.120.0140.00524.39
8.4.110.0100.01218.44
8.4.100.0160.00519.07
8.4.90.0120.00920.87
8.4.80.0050.00419.11
8.4.70.0100.00918.45
8.4.60.0070.00919.20
8.4.50.0050.00320.83
8.4.40.0110.00820.90
8.4.30.0120.00822.91
8.4.20.0040.00419.03
8.4.10.0000.01020.05
8.3.280.0120.00918.94
8.3.270.0140.00716.83
8.3.260.0120.00717.19
8.3.250.0050.00319.47
8.3.240.0060.00317.52
8.3.230.0130.00617.25
8.3.220.0140.00519.61
8.3.210.0040.00717.02
8.3.200.0030.00617.24
8.3.190.0100.01317.46
8.3.180.0110.00817.61
8.3.170.0130.00719.23
8.3.160.0000.00817.59
8.3.150.0090.00917.28
8.3.140.0060.00317.41
8.3.130.0040.00419.08
8.3.120.0030.00621.08
8.3.110.0100.01020.94
8.3.100.0080.00020.79
8.3.90.0120.00917.11
8.3.80.0050.00518.98
8.3.70.0070.00718.69
8.3.60.0090.00618.80
8.3.50.0110.01422.33
8.3.40.0180.00019.27
8.3.30.0060.00919.26
8.3.20.0080.00022.10
8.3.10.0040.00420.52
8.3.00.0040.00424.03
8.2.290.0090.00920.71
8.2.280.0110.00919.02
8.2.270.0070.01319.32
8.2.260.0140.00016.84
8.2.250.0030.00618.81
8.2.240.0000.00917.27
8.2.230.0040.00422.58
8.2.220.0070.00324.06
8.2.210.0120.00326.77
8.2.200.0100.00317.12
8.2.190.0100.01317.25
8.2.180.0090.00617.25
8.2.170.0120.00322.96
8.2.160.0070.00721.55
8.2.150.0070.00324.18
8.2.140.0050.00324.66
8.2.130.0040.00426.16
8.2.120.0050.00321.45
8.2.110.0070.00320.65
8.2.100.0060.00618.53
8.2.90.0000.00818.47
8.2.80.0040.00418.40
8.2.70.0030.00618.30
8.2.60.0090.00318.53
8.2.50.0050.00318.38
8.2.40.0050.00319.84
8.2.30.0000.00819.60
8.2.20.0040.00418.61
8.2.10.0080.00019.65
8.2.00.0000.00919.66
8.1.330.0120.00822.32
8.1.320.0150.00716.54
8.1.310.0150.00418.81
8.1.300.0090.00917.11
8.1.290.0040.00418.88
8.1.280.0110.00725.92
8.1.270.0080.00024.29
8.1.260.0090.00026.35
8.1.250.0040.00428.09
8.1.240.0030.00724.02
8.1.230.0040.00821.30
8.1.220.0000.00918.15
8.1.210.0090.00019.29
8.1.200.0000.01117.75
8.1.190.0000.00817.73
8.1.180.0030.00618.10
8.1.170.0030.00519.07
8.1.160.0020.00519.27
8.1.150.0050.00219.17
8.1.140.0000.00719.28
8.1.130.0030.00320.52
8.1.120.0030.00517.94
8.1.110.0000.00717.89
8.1.100.0000.00817.86
8.1.90.0000.00818.02
8.1.80.0030.00517.85
8.1.70.0090.00017.90
8.1.60.0050.00318.09
8.1.50.0000.00818.06
8.1.40.0060.00318.01
8.1.30.0030.00618.11
8.1.20.0060.00318.13
8.1.10.0040.00417.98
8.1.00.0040.00418.04
8.0.300.0060.00320.19
8.0.290.0040.00417.25
8.0.280.0040.00418.84
8.0.270.0000.00717.58
8.0.260.0000.00720.58
8.0.250.0030.00617.41
8.0.240.0040.00417.52
8.0.230.0050.00217.44
8.0.220.0030.00717.42
8.0.210.0030.00517.34
8.0.200.0050.00217.40
8.0.190.0040.00417.36
8.0.180.0000.00817.47
8.0.170.0040.00417.29
8.0.160.0040.00417.43
8.0.150.0040.00417.33
8.0.140.0040.00417.27
8.0.130.0000.00613.82
8.0.120.0030.00517.40
8.0.110.0070.00017.35
8.0.100.0080.00017.20
8.0.90.0030.00617.44
8.0.80.0060.00917.27
8.0.70.0080.00017.17
8.0.60.0000.00717.35
8.0.50.0040.00417.43
8.0.30.0100.01017.87
8.0.20.0110.00917.54
8.0.10.0000.00817.26
8.0.00.0080.01217.18
7.4.330.0000.00615.55
7.4.320.0060.00017.03
7.4.300.0030.00317.13
7.4.290.0030.00517.00
7.4.280.0050.00317.06
7.4.270.0080.00017.11
7.4.260.0050.00217.20
7.4.250.0040.00417.16
7.4.240.0060.00117.09
7.4.230.0050.00217.16
7.4.220.0040.00417.16
7.4.210.0070.00817.24
7.4.200.0040.00417.16
7.4.140.0120.01117.86
7.4.130.0120.00617.03
7.4.120.0100.01016.92
7.4.110.0070.01117.02
7.4.100.0120.00617.07
7.4.90.0060.01217.13
7.4.80.0070.01019.39
7.4.70.0100.01017.19
7.4.60.0110.00916.86
7.4.50.0090.00717.02
7.4.40.0140.00717.05
7.4.30.0110.00617.09
7.4.20.0090.00916.94
7.4.10.0070.01017.09
7.4.00.0090.00716.56
7.3.330.0000.00713.64
7.3.320.0000.00513.83
7.3.310.0040.00416.76
7.3.300.0000.00816.77
7.3.290.0070.00016.86
7.3.280.0090.00916.86
7.3.260.0090.01016.97
7.3.240.0130.00717.03
7.3.230.0100.00617.12
7.3.210.0110.00516.82
7.3.200.0050.01117.14
7.3.190.0160.00016.88
7.3.180.0130.00316.93
7.3.170.0140.00717.05
7.3.160.0080.01117.00
7.3.150.0040.01316.95
7.3.140.0080.01117.13
7.3.130.0100.00716.87
7.3.120.0090.00917.20
7.3.110.0070.01017.16
7.3.100.0060.01117.20
7.3.90.0100.00917.02
7.3.80.0050.01417.12
7.3.70.0100.00816.04
7.3.60.0120.00616.00
7.3.50.0100.01015.95
7.3.40.0100.00516.04
7.3.30.0040.01316.05
7.3.20.0110.00817.82
7.3.10.0080.00817.80
7.3.00.0060.01217.81
7.2.330.0070.01017.25
7.2.320.0100.01017.37
7.2.310.0070.01316.98
7.2.300.0030.01717.26
7.2.290.0070.01017.21
7.2.280.0150.00317.09
7.2.270.0060.01217.03
7.2.260.0090.00917.20
7.2.250.0100.00717.19
7.2.240.0080.01217.11
7.2.230.0140.00417.28
7.2.220.0090.00917.02
7.2.210.0060.01217.34
7.2.200.0100.01216.34
7.2.190.0100.00916.23
7.2.180.0110.01016.31
7.2.170.0070.00916.18
7.2.160.0110.00416.04
7.2.150.0110.00717.99
7.2.140.0080.00918.04
7.2.130.0070.01317.93
7.2.120.0140.00618.12
7.2.110.0020.01617.94
7.2.100.0080.00918.04
7.2.90.0110.00518.03
7.2.80.0070.01517.89
7.2.70.0100.01018.14
7.2.60.0100.00817.96
7.2.50.0100.00817.89
7.2.40.0120.00518.03
7.2.30.0120.01017.99
7.2.20.0100.00817.94
7.2.10.0070.00918.04
7.2.00.0040.01218.03
7.1.330.0130.01016.22
7.1.320.0060.01317.93
7.1.310.0100.00918.04
7.1.300.0050.01616.97
7.1.290.0110.00916.96
7.1.280.0120.01017.04
7.1.270.0080.00817.00
7.1.260.0040.01216.94
7.1.250.0130.00616.91
7.1.240.0100.00617.68
7.1.230.0110.00817.67
7.1.220.0130.00417.64
7.1.210.0040.01318.02
7.1.200.0070.01017.82
7.1.190.0100.00617.76
7.1.180.0070.01017.82
7.1.170.0090.00917.75
7.1.160.0070.01017.63
7.1.150.0080.00817.69
7.1.140.0180.00317.95
7.1.130.0100.01017.91
7.1.120.0120.00617.94
7.1.110.0090.00617.91
7.1.100.0130.01017.76
7.1.90.0040.01217.98
7.1.80.0110.01118.06
7.1.70.0070.01017.89
7.1.60.0070.01017.77
7.1.50.0120.00917.88
7.1.40.0110.01117.87
7.1.30.0030.01717.92
7.1.20.0090.01217.82
7.1.10.0120.00317.92
7.1.00.0100.00717.89

preferences:
168.55 ms | 403 KiB | 5 Q