3v4l.org

run code in 300+ PHP versions simultaneously
<?php function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) { if (is_array ( $ending )) { extract ( $ending ); } if ($considerHtml) { if (strlen ( preg_replace ( '/<.*?>/', '', $text ) ) <= $length) { return $text; } $totalLength = mb_strlen ( $ending ); $openTags = array (); $truncate = ''; preg_match_all ( '/(<\/?([\w+]+)[^>]*>)?([^<>]*)/', $text, $tags, PREG_SET_ORDER ); foreach ( $tags as $tag ) { if (! preg_match ( '/img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param/s', $tag [2] )) { if (preg_match ( '/<[\w]+[^>]*>/s', $tag [0] )) { array_unshift ( $openTags, $tag [2] ); } else if (preg_match ( '/<\/([\w]+)[^>]*>/s', $tag [0], $closeTag )) { $pos = array_search ( $closeTag [1], $openTags ); if ($pos !== false) { array_splice ( $openTags, $pos, 1 ); } } } $truncate .= $tag [1]; $contentLength = strlen ( preg_replace ( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $tag [3] ) ); if ($contentLength + $totalLength > $length) { $left = $length - $totalLength; $entitiesLength = 0; if (preg_match_all ( '/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $tag [3], $entities, PREG_OFFSET_CAPTURE )) { foreach ( $entities [0] as $entity ) { if ($entity [1] + 1 - $entitiesLength <= $left) { $left --; $entitiesLength += strlen ( $entity [0] ); } else { break; } } } $truncate .= substr ( $tag [3], 0, $left + $entitiesLength ); break; } else { $truncate .= $tag [3]; $totalLength += $contentLength; } if ($totalLength >= $length) { break; } } } else { if (strlen ( $text ) <= $length) { return $text; } else { $truncate = substr ( $text, 0, $length - strlen ( $ending ) ); } } if (! $exact) { $spacepos = strrpos ( $truncate, ' ' ); if (isset ( $spacepos )) { if ($considerHtml) { $bits = substr ( $truncate, $spacepos ); preg_match_all ( '/<\/([a-z]+)>/', $bits, $droppedTags, PREG_SET_ORDER ); if (! empty ( $droppedTags )) { foreach ( $droppedTags as $closingTag ) { if (! in_array ( $closingTag [1], $openTags )) { array_unshift ( $openTags, $closingTag [1] ); } } } } $truncate = substr ( $truncate, 0, $spacepos ); } } $truncate .= $ending; if ($considerHtml) { foreach ( $openTags as $tag ) { $truncate .= ''; } } return $truncate; } var_dump(truncate("0123456789ABC", 12));

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.01018.68
8.3.50.0100.00922.05
8.3.40.0120.00318.67
8.3.30.0090.00619.21
8.3.20.0110.00420.16
8.3.10.0040.00423.44
8.3.00.0040.00720.72
8.2.180.0130.00716.50
8.2.170.0110.00722.96
8.2.160.0100.00320.39
8.2.150.0030.00724.18
8.2.140.0080.00024.66
8.2.130.0050.00326.16
8.2.120.0040.00419.36
8.2.110.0050.00522.05
8.2.100.0080.00419.57
8.2.90.0040.00419.21
8.2.80.0050.00317.97
8.2.70.0000.01017.63
8.2.60.0040.00418.05
8.2.50.0000.00918.07
8.2.40.0000.00919.89
8.2.30.0000.00818.28
8.2.20.0000.00817.86
8.2.10.0000.00818.16
8.2.00.0000.00819.57
8.1.280.0120.00625.92
8.1.270.0000.00823.92
8.1.260.0000.00726.35
8.1.250.0000.00828.09
8.1.240.0040.00422.14
8.1.230.0080.00319.27
8.1.220.0050.00317.79
8.1.210.0030.00618.77
8.1.200.0060.00317.48
8.1.190.0040.00417.34
8.1.180.0060.00318.10
8.1.170.0000.00818.76
8.1.160.0000.00722.13
8.1.150.0070.00018.73
8.1.140.0040.00417.44
8.1.130.0030.00317.89
8.1.120.0030.00717.43
8.1.110.0040.00417.52
8.1.100.0000.00717.47
8.1.90.0050.00317.40
8.1.80.0030.00517.41
8.1.70.0030.00317.52
8.1.60.0030.00617.66
8.1.50.0030.00617.50
8.1.40.0050.00317.60
8.1.30.0060.00317.68
8.1.20.0000.00917.72
8.1.10.0000.00817.59
8.1.00.0040.00417.52
8.0.300.0030.00618.77
8.0.290.0090.00016.88
8.0.280.0040.00418.55
8.0.270.0080.00017.31
8.0.260.0060.00317.32
8.0.250.0030.00316.98
8.0.240.0000.00716.92
8.0.230.0000.00716.90
8.0.220.0000.00716.85
8.0.210.0000.00716.95
8.0.200.0000.00616.98
8.0.190.0040.00416.88
8.0.180.0040.00417.00
8.0.170.0030.00617.02
8.0.160.0050.00316.92
8.0.150.0060.00316.94
8.0.140.0030.00616.91
8.0.130.0050.00013.35
8.0.120.0000.00716.89
8.0.110.0040.00416.86
8.0.100.0080.00016.94
8.0.90.0080.00016.92
8.0.80.0050.01016.86
8.0.70.0040.00417.02
8.0.60.0000.00816.77
8.0.50.0000.00716.86
8.0.30.0080.01317.19
8.0.20.0080.01117.40
8.0.10.0030.00516.99
8.0.00.0110.00616.85
7.4.330.0000.00615.08
7.4.320.0040.00416.55
7.4.300.0040.00416.57
7.4.290.0000.00716.52
7.4.280.0000.00816.45
7.4.270.0030.00316.66
7.4.260.0070.00016.55
7.4.250.0040.00416.61
7.4.240.0040.00316.62
7.4.230.0040.00416.69
7.4.220.0110.00616.68
7.4.210.0100.00916.48
7.4.200.0000.00716.75
7.4.160.0070.01016.52
7.4.150.0060.01217.40
7.4.140.0070.01017.86
7.4.130.0070.01016.61
7.4.120.0090.00916.64
7.4.110.0100.00716.55
7.4.100.0070.01116.44
7.4.90.0100.00616.64
7.4.80.0070.01019.39
7.4.70.0150.00416.32
7.4.60.0060.01216.70
7.4.50.0060.00016.30
7.4.40.0120.00616.55
7.4.30.0060.01216.84
7.4.00.0040.01515.14
7.3.330.0000.00513.39
7.3.320.0030.00313.36
7.3.310.0050.00216.26
7.3.300.0030.00316.41
7.3.290.0080.00816.40
7.3.280.0090.01116.39
7.3.270.0100.00717.40
7.3.260.0120.00616.63
7.3.250.0090.01216.60
7.3.240.0040.01416.35
7.3.230.0060.01516.69
7.3.210.0100.01316.31
7.3.200.0090.00919.39
7.3.190.0130.00316.27
7.3.180.0130.00316.45
7.3.170.0150.00616.34
7.3.160.0150.00616.48
7.3.120.0070.01017.15
7.3.110.0090.00317.15
7.3.100.0040.00717.15
7.3.90.0090.00317.15
7.3.80.0040.01117.15
7.3.70.0000.01717.15
7.3.60.0000.01017.15
7.3.50.0000.01617.15
7.3.40.0070.00317.15
7.3.30.0070.01017.15
7.3.20.0040.00817.15
7.3.10.0030.01417.15
7.3.00.0070.01017.15
7.2.330.0130.00416.38
7.2.320.0100.00916.70
7.2.310.0070.01016.78
7.2.300.0130.00316.64
7.2.290.0130.00316.60
7.2.240.0070.00717.15
7.2.230.0040.01217.15
7.2.220.0110.00317.15
7.2.210.0030.01417.15
7.2.200.0040.00817.15
7.2.190.0040.01217.15
7.2.180.0000.01117.15
7.2.170.0040.01217.15
7.2.160.0040.01217.15
7.2.150.0070.01017.15
7.2.140.0100.00617.15
7.2.130.0100.00717.15
7.2.120.0090.00617.15
7.2.110.0080.00817.15
7.2.100.0070.00317.15
7.2.90.0060.00917.15
7.2.80.0100.00717.15
7.2.70.0070.01117.15
7.2.60.0100.00516.99
7.2.50.0030.00617.15
7.2.40.0040.01117.15
7.2.30.0070.00717.15
7.2.20.0000.01317.15
7.2.10.0000.01617.15
7.2.00.0030.01218.29
7.1.330.0090.00317.15
7.1.320.0030.01217.15
7.1.310.0030.01017.15
7.1.300.0060.00617.15
7.1.290.0100.00617.15
7.1.280.0040.01117.15
7.1.270.0000.01417.15
7.1.260.0070.01017.15
7.1.250.0000.00917.15
7.1.200.0130.00015.79
7.1.100.0060.00618.18
7.1.70.0040.00417.10
7.1.60.0130.01019.70
7.1.50.0140.01017.05
7.1.00.0030.07722.50
7.0.200.0050.00316.79
7.0.140.0030.07322.10
7.0.60.0130.07019.96
7.0.50.0030.07017.95
7.0.40.0200.07720.11
7.0.30.0430.03720.13
7.0.20.0370.04320.34
7.0.10.0070.08720.21
7.0.00.0030.09320.22
5.6.280.0100.06721.12
5.6.210.0030.08720.56
5.6.200.0030.04018.16
5.6.190.0200.04320.39
5.6.180.0270.06320.48
5.6.170.0200.05320.47
5.6.160.0100.03720.54
5.6.150.0030.04318.18
5.6.140.0130.07318.18
5.6.130.0230.06018.27
5.6.120.0070.08321.14
5.6.110.0070.08020.91
5.6.100.0130.07720.98
5.6.90.0070.06721.11
5.6.80.0130.06720.41
5.6.70.4330.03020.30
5.5.350.0030.07720.38
5.5.340.0070.04318.09
5.5.330.0030.06320.30
5.5.320.0200.07020.37
5.5.310.0300.05320.35
5.5.300.0030.03717.99
5.5.290.0100.05017.98
5.5.280.0030.04720.67
5.5.270.0100.08020.86
5.5.260.0070.08020.75
5.5.250.0100.06320.60
5.5.240.0130.07020.09
5.4.450.0300.04730.96
5.4.440.0470.04729.30
5.4.430.0270.05029.27
5.4.420.0270.04728.90
5.4.410.0300.03326.63
5.4.400.0130.04324.50
5.4.390.0370.03723.00
5.4.380.0770.06018.58
5.4.370.0770.04718.74
5.4.360.0730.04318.73
5.4.350.0670.05718.74
5.4.340.0770.05718.52
5.4.320.0070.03512.53
5.4.310.0050.03712.52
5.4.300.0100.03312.52
5.4.290.0100.04112.52
5.4.280.0080.03712.42
5.4.270.0050.03712.42
5.4.260.0050.03912.42
5.4.250.0050.03812.42
5.4.240.0070.03612.42
5.4.230.0070.05212.41
5.4.220.0030.04012.41
5.4.210.0070.03612.41
5.4.200.0040.04012.41
5.4.190.0070.03912.40
5.4.180.0030.03912.40
5.4.170.0100.04312.42
5.4.160.0050.03812.41
5.4.150.0070.03612.41
5.4.140.0040.03812.09
5.4.130.0050.03712.08
5.4.120.0090.03812.03
5.4.110.0090.03312.03
5.4.100.0070.03612.03
5.4.90.0050.03712.04
5.4.80.0060.03612.03
5.4.70.0040.03512.03
5.4.60.0060.03612.03
5.4.50.0060.03512.03
5.4.40.0080.03912.02
5.4.30.0090.03812.01
5.4.20.0040.03712.01
5.4.10.0030.03712.02
5.4.00.0050.04111.50
5.3.290.0080.04012.80
5.3.280.0060.03812.71
5.3.270.0090.03612.73
5.3.260.0080.03712.71
5.3.250.0080.03712.72
5.3.240.0080.03512.71
5.3.230.0070.04412.70
5.3.220.0060.04012.68
5.3.210.0070.03812.68
5.3.200.0080.03312.68
5.3.190.0090.03412.68
5.3.180.0070.03712.67
5.3.170.0040.04012.67
5.3.160.0060.03612.68
5.3.150.0060.04112.68
5.3.140.0080.03912.66
5.3.130.0040.04112.66
5.3.120.0100.04112.66
5.3.110.0050.04012.66
5.3.100.0030.04012.14
5.3.90.0060.04612.13
5.3.80.0070.04012.12
5.3.70.0100.04212.12
5.3.60.0050.03612.11
5.3.50.0040.03712.05
5.3.40.0070.04112.05
5.3.30.0060.03612.01
5.3.20.0030.03711.79
5.3.10.0050.03511.75
5.3.00.0080.04511.74
5.2.170.0060.0339.25
5.2.160.0050.0299.25
5.2.150.0030.0319.25
5.2.140.0050.0309.25
5.2.130.0070.0359.21
5.2.120.0080.0359.21
5.2.110.0050.0299.21
5.2.100.0060.0279.21
5.2.90.0030.0309.21
5.2.80.0020.0339.20
5.2.70.0050.0319.20
5.2.60.0070.0389.16
5.2.50.0060.0339.13
5.2.40.0040.0299.10
5.2.30.0040.0399.07
5.2.20.0040.0309.07
5.2.10.0020.0308.98
5.2.00.0050.0288.83
5.1.60.0040.0238.12
5.1.50.0050.0278.12
5.1.40.0050.0248.09
5.1.30.0040.0268.45
5.1.20.0060.0298.47
5.1.10.0080.0288.19
5.1.00.0090.0268.19
5.0.50.0020.0216.68
5.0.40.0050.0176.54
5.0.30.0040.0306.36
5.0.20.0070.0256.32
5.0.10.0040.0296.30
5.0.00.0040.0306.29
4.4.90.0010.0174.78
4.4.80.0040.0144.75
4.4.70.0000.0214.76
4.4.60.0060.0204.75
4.4.50.0030.0254.77
4.4.40.0020.0324.71
4.4.30.0020.0164.76
4.4.20.0030.0174.84
4.4.10.0050.0134.85
4.4.00.0040.0294.76
4.3.110.0020.0164.67
4.3.100.0040.0194.66
4.3.90.0060.0114.63
4.3.80.0000.0264.59
4.3.70.0040.0174.63
4.3.60.0040.0234.63
4.3.50.0030.0224.63
4.3.40.0050.0334.54
4.3.30.0000.0183.34
4.3.20.0030.0153.32
4.3.10.0020.0163.28
4.3.00.0030.01322.40

preferences:
44.24 ms | 401 KiB | 5 Q