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");

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.0070.01418.43
8.3.50.0120.00321.21
8.3.40.0140.00719.27
8.3.30.0090.00919.22
8.3.20.0050.00320.34
8.3.10.0000.00821.95
8.3.00.0000.00820.46
8.2.180.0120.00316.75
8.2.170.0060.00922.96
8.2.160.0110.00419.19
8.2.150.0080.00024.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0090.00019.61
8.2.110.0060.00322.18
8.2.100.0080.00420.01
8.2.90.0040.00419.34
8.2.80.0030.00517.97
8.2.70.0000.01317.88
8.2.60.0040.00417.80
8.2.50.0030.00618.07
8.2.40.0030.00620.07
8.2.30.0040.00418.21
8.2.20.0080.00017.86
8.2.10.0000.00717.92
8.2.00.0080.00017.87
8.1.280.0070.00725.92
8.1.270.0040.00422.26
8.1.260.0040.00426.35
8.1.250.0050.00328.09
8.1.240.0040.00423.73
8.1.230.0070.00419.17
8.1.220.0000.00817.74
8.1.210.0050.00318.77
8.1.200.0100.00017.73
8.1.190.0000.00917.53
8.1.180.0030.00518.82
8.1.170.0030.00518.67
8.1.160.0040.00422.23
8.1.150.0050.00218.87
8.1.140.0040.00417.68
8.1.130.0030.00317.87
8.1.120.0000.00817.69
8.1.110.0000.00817.70
8.1.100.0000.00717.73
8.1.90.0000.00717.68
8.1.80.0000.00817.55
8.1.70.0000.00717.63
8.1.60.0090.00017.65
8.1.50.0000.00817.74
8.1.40.0030.00517.68
8.1.30.0000.00817.80
8.1.20.0000.00817.75
8.1.10.0040.00417.77
8.1.00.0080.00017.57
8.0.300.0000.00818.77
8.0.290.0070.00017.28
8.0.280.0070.00018.45
8.0.270.0000.00717.38
8.0.260.0000.00716.96
8.0.250.0040.00417.09
8.0.240.0040.00417.15
8.0.230.0070.00017.17
8.0.220.0000.00817.13
8.0.210.0030.00516.94
8.0.200.0000.00717.00
8.0.190.0000.00717.10
8.0.180.0000.00817.00
8.0.170.0000.00817.10
8.0.160.0040.00417.02
8.0.150.0080.00017.08
8.0.140.0040.00417.01
8.0.130.0030.00313.48
8.0.120.0040.00417.04
8.0.110.0050.00317.09
8.0.100.0040.00417.02
8.0.90.0050.00217.12
8.0.80.0110.01116.96
8.0.70.0000.00717.02
8.0.60.0070.00016.85
8.0.50.0030.00517.10
8.0.30.0090.01017.54
8.0.20.0170.01017.49
8.0.10.0000.00817.09
8.0.00.0090.01017.12
7.4.330.0000.00515.00
7.4.320.0000.00616.79
7.4.300.0000.00616.86
7.4.290.0030.00316.86
7.4.280.0080.00016.84
7.4.270.0000.00816.77
7.4.260.0050.00316.84
7.4.250.0000.00816.59
7.4.240.0020.00616.84
7.4.230.0000.00816.81
7.4.220.0090.00616.75
7.4.210.0060.00916.80
7.4.200.0040.00416.65
7.4.190.0070.00016.80
7.4.160.0060.01817.00
7.4.150.0120.00617.40
7.4.140.0070.01117.86
7.4.130.0120.00716.83
7.4.120.0070.01216.69
7.4.110.0140.00316.54
7.4.100.0150.00716.90
7.4.90.0050.01616.73
7.4.80.0120.00619.39
7.4.70.0070.01316.88
7.4.60.0120.00616.69
7.4.50.0000.00916.71
7.4.40.0060.00922.77
7.4.30.0100.01216.60
7.4.00.0110.00715.06
7.3.330.0030.00313.54
7.3.320.0000.00613.60
7.3.310.0000.00716.61
7.3.300.0040.00416.48
7.3.290.0120.00316.57
7.3.280.0090.00916.60
7.3.270.0130.00417.40
7.3.260.0080.00816.48
7.3.250.0100.00816.69
7.3.240.0080.00816.57
7.3.230.0030.01316.62
7.3.210.0120.00616.80
7.3.200.0140.00219.39
7.3.190.0120.00516.75
7.3.180.0100.00616.70
7.3.170.0100.01016.77
7.3.160.0120.00316.59
7.3.120.0040.01115.02
7.3.10.0030.00916.84
7.3.00.0070.00716.86
7.2.330.0070.01016.77
7.2.320.0140.00516.74
7.2.310.0100.01316.97
7.2.300.0080.00817.03
7.2.290.0080.00816.76
7.2.130.0040.00717.04
7.2.120.0030.00917.28
7.2.110.0000.01117.14
7.2.100.0100.00016.74
7.2.90.0030.01016.96
7.2.80.0110.00017.07
7.2.70.0100.00017.12
7.2.60.0000.01516.85
7.2.50.0120.00417.12
7.2.40.0000.01116.93
7.2.30.0000.01317.36
7.2.20.0060.00616.78
7.2.10.0070.00317.20
7.2.00.0060.00718.31
7.1.250.0090.00316.04
7.1.200.0090.00616.13
7.1.100.0060.00618.36
7.1.70.0040.00417.38
7.1.60.0040.01119.82
7.1.50.0080.00617.20
7.1.00.0000.08022.36
7.0.200.0000.01116.91
7.0.140.0070.07022.09
7.0.100.0330.02719.96
7.0.90.0200.05720.02
7.0.80.0170.06719.95
7.0.70.0030.04319.93
7.0.60.0170.07319.97
7.0.50.0100.03720.25
7.0.40.0030.08720.09
7.0.30.0100.03020.12
7.0.20.0070.08720.05
7.0.10.0070.04720.02
7.0.00.0070.04320.16
5.6.280.0100.06721.00
5.6.250.0170.06320.77
5.6.240.0100.08020.70
5.6.230.0030.04320.61
5.6.220.0130.07320.66
5.6.210.0000.07720.78
5.6.200.0070.07321.15
5.6.190.0030.09320.97
5.6.180.0100.07021.05
5.6.170.0100.09021.13
5.6.160.0000.08321.01
5.6.150.0070.03721.09
5.6.140.0070.08720.96
5.6.130.0030.04021.05
5.6.120.0070.08721.02
5.6.110.0030.06321.09
5.6.100.0030.08321.02
5.6.90.0070.07020.95
5.6.80.0070.07320.34
5.6.70.0200.08020.33
5.6.60.0130.07020.38
5.6.50.0100.07020.36
5.6.40.0170.02720.31
5.6.30.0000.04320.33
5.6.20.0100.06020.45
5.6.10.0030.03720.42
5.6.00.0000.06320.41
5.5.380.0100.04020.47
5.5.370.0000.05320.52
5.5.360.0030.07720.40
5.5.350.0200.03020.44
5.5.340.0170.06020.88
5.5.330.0070.07020.82
5.5.320.0070.08020.87
5.5.310.0070.03320.94
5.5.300.0030.07020.75
5.5.290.0100.07720.90
5.5.280.0030.04320.75
5.5.270.0030.08320.84
5.5.260.0070.05720.91
5.5.250.0300.06020.75
5.5.240.0070.03320.12
5.5.230.0100.03720.28
5.5.220.0030.07720.32
5.5.210.0130.03720.17
5.5.200.0000.05320.26
5.5.190.0070.03720.25
5.5.180.0070.06020.13
5.5.160.0070.04320.01
5.5.150.0100.07720.24
5.5.140.0030.05320.29
5.5.130.0030.06019.97
5.5.120.0170.07720.25
5.5.110.0100.07020.00
5.5.100.0070.03320.19
5.5.90.0000.05020.16
5.5.80.0100.03720.17
5.5.70.0000.08019.96
5.5.60.0030.08020.05
5.5.50.0170.02320.08
5.5.40.0130.06319.98
5.5.30.0030.07320.10
5.5.20.0130.07320.05
5.5.10.0130.07020.04
5.5.00.0030.03720.06
5.4.450.0000.09019.21
5.4.440.0030.04019.27
5.4.430.0070.05719.17
5.4.420.0100.07719.45
5.4.410.0030.07719.32
5.4.400.0030.03319.13
5.4.390.0100.07018.88
5.4.380.0070.07319.04
5.4.370.0070.08018.91
5.4.360.0030.08319.14
5.4.350.0130.05319.04
5.4.340.0130.02719.25
5.4.320.0030.04319.13
5.4.310.0030.06319.20
5.4.300.0070.06319.09
5.4.290.0100.06019.13
5.4.280.0130.06019.03
5.4.270.0030.06019.02
5.4.260.0270.03019.13
5.4.250.0070.07719.11
5.4.240.0030.03319.08
5.4.230.0000.03719.04
5.4.220.0030.08319.13
5.4.210.0100.07019.18
5.4.200.0000.07719.23
5.4.190.0000.03718.86
5.4.180.0100.06718.89
5.4.170.0100.07319.13
5.4.160.0070.07719.01
5.4.150.0130.03019.19
5.4.140.0000.07716.46
5.4.130.0000.08016.49
5.4.120.0030.07016.45
5.4.110.0030.07016.43
5.4.100.0000.08316.42
5.4.90.0070.07016.50
5.4.80.0100.07716.41
5.4.70.0070.06316.43
5.4.60.0000.03716.32
5.4.50.0030.04316.42
5.4.40.0070.03016.47
5.4.30.0070.03316.47
5.4.20.0030.05316.32
5.4.10.0070.04716.46
5.4.00.0070.02715.90
5.3.290.0170.02314.84
5.3.280.0030.05314.62
5.3.270.0030.03714.59
5.3.260.0000.08314.68
5.3.250.0100.06314.68
5.3.240.0030.03314.69
5.3.230.0100.06714.63
5.3.220.0000.04714.65
5.3.210.0100.06714.59
5.3.200.0070.06314.73
5.3.190.0000.03714.70
5.3.180.0030.04314.59
5.3.170.0000.07014.57
5.3.160.0030.03714.54
5.3.150.0130.02714.68
5.3.140.0030.07314.57
5.3.130.0100.03714.55
5.3.120.0070.07314.62
5.3.110.0030.04014.54
5.3.100.0100.07714.05
5.3.90.0070.06314.01
5.3.80.0100.04314.13
5.3.70.0130.06714.13
5.3.60.0070.06013.95
5.3.50.0000.03314.11
5.3.40.0070.03714.07
5.3.30.0070.04713.95
5.3.20.0030.03013.64
5.3.10.0000.03713.73
5.3.00.0030.03713.77
5.2.170.0070.05311.03
5.2.160.0030.06011.22
5.2.150.0070.04311.07
5.2.140.0030.04011.09
5.2.130.0100.02311.26
5.2.120.0030.03011.21
5.2.110.0070.05010.98
5.2.100.0100.04311.17
5.2.90.0030.03711.17
5.2.80.0070.02711.23
5.2.70.0000.03711.00
5.2.60.0070.05311.06
5.2.50.0030.02311.15
5.2.40.0000.02711.14
5.2.30.0030.02310.91
5.2.20.0100.04710.85
5.2.10.0100.04710.98
5.2.00.0030.06310.83
5.1.60.0100.04710.48
5.1.50.0030.03010.48
5.1.40.0000.03310.48
5.1.30.0070.04010.48
5.1.20.0000.03310.48
5.1.10.0100.03710.48
5.1.00.0030.05310.48
5.0.50.0000.03710.48
5.0.40.0000.03310.48
5.0.30.0000.06710.48
5.0.20.0000.02010.48
5.0.10.0000.02310.48
5.0.00.0000.03310.48
4.4.90.0030.02010.48
4.4.80.0000.02710.48
4.4.70.0070.02310.48
4.4.60.0070.02710.48
4.4.50.0000.02710.48
4.4.40.0000.02710.48
4.4.30.0000.03310.48
4.4.20.0030.01710.48
4.4.10.0030.03010.48
4.4.00.0000.03310.48
4.3.110.0000.03010.48
4.3.100.0030.01310.48
4.3.90.0070.00710.48
4.3.80.0070.02010.48
4.3.70.0030.01310.48
4.3.60.0000.01710.48
4.3.50.0030.01310.48
4.3.40.0000.02010.48
4.3.30.0070.01010.48
4.3.20.0000.01710.48
4.3.10.0000.01710.48
4.3.00.0000.01710.48

preferences:
51.08 ms | 401 KiB | 5 Q