3v4l.org

run code in 300+ PHP versions simultaneously
<?php function printTruncated($maxLength, $html, $isUtf8=true) { $printedLength = 0; $position = 0; $tags = array(); // For UTF-8, we need to count multibyte sequences as one character. $re = $isUtf8 ? '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;|[\x80-\xFF][\x80-\xBF]*}' : '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}'; while ($printedLength < $maxLength && preg_match($re, $html, $match, PREG_OFFSET_CAPTURE, $position)) { list($tag, $tagPosition) = $match[0]; // Print text leading up to the tag. $str = substr($html, $position, $tagPosition - $position); if ($printedLength + strlen($str) > $maxLength) { print(substr($str, 0, $maxLength - $printedLength)); $printedLength = $maxLength; break; } print($str); $printedLength += strlen($str); if ($printedLength >= $maxLength) break; if ($tag[0] == '&' || ord($tag) >= 0x80) { // Pass the entity or UTF-8 multibyte sequence through unchanged. print($tag); $printedLength++; } else { // Handle the tag. $tagName = $match[1][0]; if ($tag[1] == '/') { // This is a closing tag. $openingTag = array_pop($tags); assert($openingTag == $tagName); // check that tags are properly nested. print($tag); } else if ($tag[strlen($tag) - 2] == '/') { // Self-closing tag. print($tag); } else { // Opening tag. print($tag); $tags[] = $tagName; } } // Continue after the tag. $position = $tagPosition + strlen($tag); } // Print any remaining text. if ($printedLength < $maxLength && $position < strlen($html)) print(substr($html, $position, $maxLength - $printedLength)); // Close any open tags. while (!empty($tags)) printf('</%s>', array_pop($tags)); } printTruncated(10, '<b>&lt;Hello&gt;</b> <img src="world.png" alt="" /> world!'); print("\n"); printTruncated(10, '<table><tr><td>Heck, </td><td>throw</td></tr><tr><td>in a</td><td>table</td></tr></table>'); print("\n"); printTruncated(10, "<em><b>Hello</b>&#20;w\xC3\xB8rld!</em>"); print("\n"); printTruncated(500, "<div>AAAAAAAAA</div> <div><ol><li>xxxxxxxxxxxxxxx</li><li>yyyyyyyy<b>yyyyyyyyy</b>yyyyyyyy</li><li>zzzzzzz</li></ol>ccccccccccccccccccccccccccccccccccc<br> cccccccccccccccccccccccccccccccccccccccc<br> cccccccccccccccccccccccccccccccccccccccc<br> cccccccccccccccccccccccccccccccccccccccc </div>"); print("\n");

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.0100.01017.00
8.3.50.0080.00816.81
8.3.40.0070.01419.08
8.3.30.0120.00318.95
8.3.20.0000.00820.34
8.3.10.0090.00023.70
8.3.00.0040.00421.61
8.2.180.0090.01216.63
8.2.170.0180.00022.96
8.2.160.0140.00020.35
8.2.150.0030.00524.18
8.2.140.0030.00524.66
8.2.130.0040.00421.12
8.2.120.0040.00426.35
8.2.110.0070.00321.09
8.2.100.0030.00918.05
8.2.90.0000.00819.17
8.2.80.0040.00417.97
8.2.70.0060.00317.86
8.2.60.0000.00818.05
8.2.50.0080.00318.09
8.2.40.0030.00522.27
8.2.30.0000.00819.63
8.2.20.0000.00918.05
8.2.10.0000.00718.13
8.2.00.0040.00418.21
8.1.280.0040.01125.92
8.1.270.0000.00822.20
8.1.260.0080.00028.09
8.1.250.0050.00328.09
8.1.240.0030.00622.50
8.1.230.0040.00817.83
8.1.220.0050.00517.74
8.1.210.0050.00318.77
8.1.200.0000.00917.60
8.1.190.0030.00617.60
8.1.180.0000.00818.10
8.1.170.0060.00318.79
8.1.160.0080.00020.76
8.1.150.0000.00718.93
8.1.140.0030.00619.63
8.1.130.0040.00417.64
8.1.120.0040.00417.60
8.1.110.0040.00417.61
8.1.100.0060.00317.58
8.1.90.0000.00817.55
8.1.80.0060.00317.68
8.1.70.0000.00817.59
8.1.60.0080.00317.73
8.1.50.0030.00617.74
8.1.40.0050.00317.70
8.1.30.0030.00517.76
8.1.20.0040.00417.80
8.1.10.0050.00317.65
8.1.00.0050.00317.55
8.0.300.0000.00818.77
8.0.290.0030.00616.71
8.0.280.0000.00718.51
8.0.270.0050.00317.25
8.0.260.0040.00417.32
8.0.250.0070.00017.10
8.0.240.0000.00717.07
8.0.230.0030.00517.16
8.0.220.0000.00817.09
8.0.210.0040.00417.01
8.0.200.0030.00317.03
8.0.190.0000.00817.21
8.0.180.0000.00717.11
8.0.170.0030.00617.18
8.0.160.0060.00317.16
8.0.150.0040.00417.12
8.0.140.0040.00417.07
8.0.130.0060.00013.59
8.0.120.0050.00317.09
8.0.110.0000.00817.07
8.0.100.0050.00317.04
8.0.90.0060.00317.18
8.0.80.0030.01217.12
8.0.70.0040.00417.05
8.0.60.0050.00317.20
8.0.50.0000.00817.12
8.0.30.0130.00817.32
8.0.20.0100.01217.41
8.0.10.0040.00417.32
8.0.00.0120.00916.89
7.4.330.0000.00616.69
7.4.320.0060.00016.86
7.4.300.0050.00316.80
7.4.290.0000.00816.80
7.4.280.0040.00416.82
7.4.270.0000.00716.86
7.4.260.0060.00616.81
7.4.250.0030.00616.71
7.4.240.0000.00716.75
7.4.230.0020.00516.63
7.4.220.0060.01916.80
7.4.210.0110.00716.72
7.4.200.0000.00716.68
7.4.160.0080.01116.73
7.4.150.0210.00317.40
7.4.140.0150.01117.86
7.4.130.0070.01016.79
7.4.120.0130.00716.83
7.4.110.0120.01116.87
7.4.100.0110.00716.68
7.4.90.0110.00716.75
7.4.80.0130.00319.39
7.4.70.0040.01316.79
7.4.60.0120.01016.74
7.4.50.0120.00416.64
7.4.40.0080.01116.60
7.4.30.0120.00616.85
7.4.00.0000.01315.05
7.3.330.0060.00013.62
7.3.320.0030.00313.55
7.3.310.0030.00316.59
7.3.300.0040.00416.60
7.3.290.0080.00816.62
7.3.280.0080.00916.60
7.3.270.0090.00917.40
7.3.260.0130.00616.90
7.3.250.0090.01016.70
7.3.240.0130.00716.60
7.3.230.0100.00716.82
7.3.210.0070.01016.68
7.3.200.0140.00719.39
7.3.190.0070.01416.67
7.3.180.0100.01016.57
7.3.170.0070.01516.64
7.3.160.0140.00316.72
7.3.120.0100.01014.97
7.3.110.0040.01414.88
7.3.100.0000.01115.25
7.3.90.0110.00415.05
7.3.80.0040.00715.18
7.3.70.0040.01114.73
7.3.60.0030.00715.11
7.3.50.0000.01315.10
7.3.40.0070.00714.82
7.3.30.0150.00014.84
7.3.20.0030.00616.91
7.3.10.0030.01016.92
7.3.00.0080.00816.71
7.2.330.0090.01216.75
7.2.320.0070.01116.85
7.2.310.0090.00816.76
7.2.300.0120.01116.95
7.2.290.0060.01316.98
7.2.240.0060.01315.35
7.2.230.0030.01215.15
7.2.220.0070.00715.41
7.2.210.0060.00615.05
7.2.200.0030.01015.37
7.2.190.0030.00615.03
7.2.180.0030.00915.31
7.2.170.0030.01015.22
7.2.160.0040.01115.16
7.2.150.0130.00016.97
7.2.140.0070.00717.20
7.2.130.0110.00717.15
7.2.120.0060.01017.15
7.2.110.0000.00916.96
7.2.100.0040.00816.88
7.2.90.0040.00816.99
7.2.80.0160.00017.14
7.2.70.0040.00717.28
7.2.60.0070.00717.05
7.2.50.0030.00717.23
7.2.40.0070.01116.72
7.2.30.0030.01416.91
7.2.20.0000.01516.98
7.2.10.0060.00316.84
7.2.00.0020.01118.21
7.1.330.0060.01016.01
7.1.320.0100.00615.91
7.1.310.0000.01116.14
7.1.300.0000.01016.09
7.1.290.0030.00915.93
7.1.280.0060.00616.07
7.1.270.0030.00515.94
7.1.260.0040.01215.96
7.1.250.0070.00715.89
7.1.200.0000.01216.05
7.1.100.0040.01118.18
7.1.70.0030.01317.43
7.1.60.0060.01919.40
7.1.50.0040.01817.21
7.1.00.0030.07722.43
7.0.200.0170.00314.84
7.0.70.0000.07321.93
7.0.60.0000.03721.86
7.0.50.0070.08322.14
7.0.40.0030.05720.18
7.0.30.0070.08320.17
7.0.20.0030.06719.95
7.0.10.0030.09020.02
7.0.00.0070.04020.18
5.6.280.0000.03320.88
5.6.220.0000.04720.74
5.6.210.0070.03320.66
5.6.200.0000.05721.10
5.6.190.0130.07721.12
5.6.180.0070.08321.05
5.6.170.0000.09021.04
5.6.160.0070.07321.04
5.6.150.0000.08721.04
5.6.140.0100.08021.12
5.6.130.0000.08321.04
5.6.120.0030.07021.03
5.6.110.0030.06020.96
5.6.100.0170.07321.05
5.6.90.0070.07020.93
5.6.80.0130.06720.45
5.6.70.0000.08720.43
5.6.60.0100.06720.49
5.6.50.0130.06720.42
5.6.40.0070.07720.33
5.6.30.0000.05720.45
5.6.20.0030.07720.48
5.6.10.0070.08020.34
5.6.00.0070.08020.39
5.5.360.0130.03020.57
5.5.350.0030.03320.44
5.5.340.0030.05720.88
5.5.330.0000.06720.82
5.5.320.0070.07320.82
5.5.310.0000.06320.70
5.5.300.0130.06720.93
5.5.290.0070.08720.88
5.5.280.0000.08020.63
5.5.270.0030.09320.65
5.5.260.0030.04320.85
5.5.250.0070.04720.56
5.5.240.0030.08720.18
5.5.230.0030.08320.25
5.5.220.0030.07720.26
5.5.210.0030.07720.31
5.5.200.0100.06720.13
5.5.190.0070.04320.25
5.5.180.0000.04320.17
5.5.160.0100.07720.22
5.5.150.0100.07720.13
5.5.140.0070.08020.10
5.5.130.0000.08020.27
5.5.120.0030.08020.15
5.5.110.0100.08020.16
5.5.100.0070.07020.07
5.5.90.0030.06720.11
5.5.80.0100.07720.09
5.5.70.0000.07020.10
5.5.60.0070.08020.07
5.5.50.0030.07019.95
5.5.40.0100.06720.07
5.5.30.0030.05320.06
5.5.20.0030.04719.95
5.5.10.0070.07320.02
5.5.00.0070.08020.06
5.4.450.0070.08019.36
5.4.440.0070.06019.53
5.4.430.0100.07319.20
5.4.420.0030.06719.48
5.4.410.0030.08319.33
5.4.400.0070.06319.17
5.4.390.0070.07718.95
5.4.380.0000.08319.23
5.4.370.0070.07718.85
5.4.360.0000.08319.13
5.4.350.0100.07318.85
5.4.340.0070.07719.18
5.4.320.0030.08019.09
5.4.310.0130.06719.23
5.4.300.0070.07319.03
5.4.290.0130.06719.12
5.4.280.0030.07719.16
5.4.270.0070.08019.12
5.4.260.0030.05319.16
5.4.250.0030.05019.12
5.4.240.0100.06719.18
5.4.230.0030.08019.14
5.4.220.0130.07319.20
5.4.210.0030.08019.02
5.4.200.0070.07019.02
5.4.190.0100.05319.02
5.4.180.0070.04719.17
5.4.170.0030.07719.19
5.4.160.0030.07019.21
5.4.150.0070.06019.01
5.4.140.0100.07316.46
5.4.130.0030.07716.45
5.4.120.0030.07716.42
5.4.110.0100.07016.38
5.4.100.0030.07716.28
5.4.90.0070.07316.39
5.4.80.0000.07716.32
5.4.70.0030.06016.48
5.4.60.0070.07316.26
5.4.50.0000.07716.48
5.4.40.0100.05316.45
5.4.30.0000.05016.45
5.4.20.0100.06716.25
5.4.10.0030.05016.47
5.4.00.0030.04715.86
5.3.290.0000.08314.65
5.3.280.0030.07314.59
5.3.270.0070.04014.71
5.3.260.0070.08014.61
5.3.250.0030.07314.68
5.3.240.0030.07314.68
5.3.230.0070.07014.58
5.3.220.0070.07714.54
5.3.210.0030.08014.65
5.3.200.0030.06714.59
5.3.190.0000.06714.65
5.3.180.0000.07714.51
5.3.170.0070.07314.67
5.3.160.0070.06714.57
5.3.150.0070.04714.64
5.3.140.0000.05714.51
5.3.130.0030.05714.52
5.3.120.0030.06014.62
5.3.110.0070.07014.59
5.3.100.0100.05314.00
5.3.90.0030.04714.05
5.3.80.0070.05014.18
5.3.70.0070.04314.18
5.3.60.0070.07313.95
5.3.50.0000.06314.02
5.3.40.0070.07713.98
5.3.30.0100.06014.03
5.3.20.0030.06313.77
5.3.10.0030.07313.74
5.3.00.0000.04313.71
5.2.170.0000.06712.41
5.2.160.0000.06012.41
5.2.150.0030.06012.41
5.2.140.0100.05312.41
5.2.130.0030.06012.41
5.2.120.0030.06012.41
5.2.110.0070.04012.41
5.2.100.0000.03712.41
5.2.90.0000.05712.41
5.2.80.0070.06012.41
5.2.70.0000.06012.41
5.2.60.0000.06712.41
5.2.50.0030.06312.41
5.2.40.0070.05012.41
5.2.30.0070.04712.41
5.2.20.0070.05712.41
5.2.10.0000.06712.41
5.2.00.0000.06012.41
5.1.60.0000.05712.41
5.1.50.0000.06012.41
5.1.40.0130.04712.41
5.1.30.0070.05312.41
5.1.20.0070.05312.41
5.1.10.0000.05712.41
5.1.00.0000.05712.41
5.0.50.0030.04312.41
5.0.40.0000.04312.41
5.0.30.0030.07012.41
5.0.20.0100.03312.41
5.0.10.0070.04012.41
5.0.00.0070.06312.41
4.4.90.0030.02012.41
4.4.80.0030.02312.41
4.4.70.0030.02012.41
4.4.60.0000.02312.41
4.4.50.0030.03012.41
4.4.40.0000.05312.41
4.4.30.0070.03012.41
4.4.20.0030.03312.41
4.4.10.0030.03012.41
4.4.00.0000.05712.41
4.3.110.0030.02712.41
4.3.100.0000.03012.41
4.3.90.0000.02312.41
4.3.80.0000.05312.41
4.3.70.0070.02712.41
4.3.60.0070.02712.41
4.3.50.0070.03012.41
4.3.40.0070.04712.41
4.3.30.0000.03312.41
4.3.20.0070.03012.41
4.3.10.0070.03712.41
4.3.00.0000.02712.41

preferences:
61.44 ms | 401 KiB | 5 Q