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(20, "<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.0110.00916.88
8.3.50.0100.01221.23
8.3.40.0080.00819.15
8.3.30.0130.00319.09
8.3.20.0090.00020.25
8.3.10.0000.00823.41
8.3.00.0000.00821.60
8.2.180.0070.01018.30
8.2.170.0120.00322.96
8.2.160.0030.01020.35
8.2.150.0040.00424.18
8.2.140.0080.00024.66
8.2.130.0030.00521.16
8.2.120.0040.00426.35
8.2.110.0090.01221.09
8.2.100.0090.00318.16
8.2.90.0000.00919.30
8.2.80.0050.00318.00
8.2.70.0030.00617.87
8.2.60.0060.00317.93
8.2.50.0040.00418.09
8.2.40.0000.00822.36
8.2.30.0030.00519.47
8.2.20.0000.00917.93
8.2.10.0000.00818.13
8.2.00.0040.00418.23
8.1.280.0030.01725.92
8.1.270.0030.00522.23
8.1.260.0040.00428.09
8.1.250.0040.00428.09
8.1.240.0090.00022.57
8.1.230.0040.00719.14
8.1.220.0000.00818.03
8.1.210.0040.00418.77
8.1.200.0040.00417.60
8.1.190.0080.00017.38
8.1.180.0040.00418.10
8.1.170.0000.00918.82
8.1.160.0050.00320.86
8.1.150.0040.00418.86
8.1.140.0040.00419.68
8.1.130.0070.00017.80
8.1.120.0050.00217.66
8.1.110.0040.00417.67
8.1.100.0060.00317.61
8.1.90.0000.00817.66
8.1.80.0000.00817.64
8.1.70.0000.00717.70
8.1.60.0060.00317.80
8.1.50.0040.00417.69
8.1.40.0040.00417.71
8.1.30.0030.00617.86
8.1.20.0040.00417.88
8.1.10.0000.00817.74
8.1.00.0060.00317.73
8.0.300.0000.00818.77
8.0.290.0040.00417.00
8.0.280.0030.00318.41
8.0.270.0030.00317.32
8.0.260.0000.00717.21
8.0.250.0030.00317.16
8.0.240.0040.00417.18
8.0.230.0030.00317.08
8.0.220.0040.00417.00
8.0.210.0000.00816.98
8.0.200.0050.00217.10
8.0.190.0000.00817.20
8.0.180.0000.00716.97
8.0.170.0040.00717.11
8.0.160.0050.00317.01
8.0.150.0040.00417.02
8.0.140.0030.00617.04
8.0.130.0030.00313.48
8.0.120.0080.00017.09
8.0.110.0000.00717.09
8.0.100.0030.00516.95
8.0.90.0040.00417.07
8.0.80.0130.00917.04
8.0.70.0030.00516.86
8.0.60.0040.00416.89
8.0.50.0040.00416.97
8.0.30.0100.00617.26
8.0.20.0100.01017.48
8.0.10.0000.00817.26
8.0.00.0100.01117.05
7.4.330.0050.00016.79
7.4.320.0000.00716.88
7.4.300.0030.00316.75
7.4.290.0000.00716.73
7.4.280.0050.00216.75
7.4.270.0000.00616.74
7.4.260.0020.01016.71
7.4.250.0040.00416.64
7.4.240.0080.00016.77
7.4.230.0040.00416.79
7.4.220.0120.00616.92
7.4.210.0070.01016.82
7.4.200.0040.00416.81
7.4.160.0100.00716.52
7.4.150.0150.00317.40
7.4.140.0110.01117.86
7.4.130.0090.01116.81
7.4.120.0110.00816.76
7.4.110.0000.01816.84
7.4.100.0120.00616.83
7.4.90.0110.00716.70
7.4.80.0110.00719.39
7.4.70.0140.00516.98
7.4.60.0090.00916.67
7.4.50.0000.01716.80
7.4.40.0060.01216.81
7.4.30.0060.01316.75
7.4.00.0070.00915.20
7.3.330.0070.00013.54
7.3.320.0060.00013.39
7.3.310.0080.00016.60
7.3.300.0000.00716.58
7.3.290.0060.00916.64
7.3.280.0070.01016.62
7.3.270.0040.01817.40
7.3.260.0140.00316.92
7.3.250.0150.00416.61
7.3.240.0110.00716.80
7.3.230.0060.01216.54
7.3.210.0110.00716.68
7.3.200.0070.01419.39
7.3.190.0100.01316.62
7.3.180.0130.00316.67
7.3.170.0120.00916.86
7.3.160.0110.00516.70
7.3.120.0150.00415.15
7.3.110.0060.01315.25
7.3.100.0130.00314.84
7.3.90.0050.00515.15
7.3.80.0070.01014.89
7.3.70.0080.00415.05
7.3.60.0070.00314.95
7.3.50.0100.00615.11
7.3.40.0060.00315.01
7.3.30.0030.00615.18
7.3.20.0080.00616.81
7.3.10.0060.00616.71
7.3.00.0040.01016.89
7.2.330.0130.01016.65
7.2.320.0100.01016.70
7.2.310.0040.01416.95
7.2.300.0110.00616.85
7.2.290.0140.00316.88
7.2.250.0060.01315.30
7.2.240.0070.01115.24
7.2.230.0110.00415.25
7.2.220.0000.01415.13
7.2.210.0000.01315.43
7.2.200.0030.01315.43
7.2.190.0030.01015.37
7.2.180.0060.00915.37
7.2.170.0000.01115.08
7.2.130.0090.00916.74
7.2.120.0100.01017.11
7.2.110.0040.01316.71
7.2.100.0040.00817.04
7.2.90.0080.00417.23
7.2.80.0010.01016.80
7.2.70.0250.00616.51
7.2.60.0060.00717.13
7.2.50.0240.00616.60
7.2.40.0180.00717.07
7.2.30.0300.00016.80
7.2.20.0160.01316.91
7.2.10.0240.00916.98
7.2.00.0130.00318.44
7.1.330.0120.00316.18
7.1.320.0070.00715.75
7.1.310.0070.00715.85
7.1.300.0070.00716.07
7.1.290.0090.00615.91
7.1.280.0000.00915.95
7.1.270.0030.01016.04
7.1.260.0080.00415.96
7.1.250.0030.00615.77
7.1.200.0030.00816.01
7.1.100.0030.00718.44
7.1.70.0120.00317.40
7.1.60.0070.01719.40
7.1.50.0060.01616.89
7.1.00.0070.07322.46
7.0.200.0180.00414.98
7.0.60.0200.06721.84
7.0.50.0100.08017.90
7.0.40.0130.07319.98
7.0.30.0270.08020.14
7.0.20.0430.07720.15
7.0.10.0000.05320.26
7.0.00.0000.04720.11
5.6.280.0000.03321.16
5.6.210.0000.06720.62
5.6.200.0100.06718.14
5.6.190.0030.05020.41
5.6.180.0270.07720.46
5.6.170.0500.06320.71
5.6.160.0030.08720.61
5.6.150.0030.04718.18
5.6.140.0100.08018.15
5.6.130.0030.05018.12
5.6.120.0130.04021.10
5.6.110.0030.08021.16
5.6.100.0100.08321.00
5.6.90.0100.07321.00
5.6.80.0030.08720.55
5.5.350.0330.07720.43
5.5.340.0100.07718.08
5.5.330.0170.07720.55
5.5.320.0600.07020.31
5.5.310.0200.05320.23
5.5.300.0130.07317.96
5.5.290.0100.07017.98
5.5.280.0100.06021.00
5.5.270.0100.08320.89
5.5.260.0030.04320.81
5.5.250.0170.07020.51
5.5.240.0170.08020.20

preferences:
34.04 ms | 401 KiB | 5 Q