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

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.40.0150.00018.69
8.3.30.0110.00618.95
8.3.20.0040.00420.31
8.3.10.0080.00023.70
8.3.00.0100.00018.89
8.2.170.0120.00322.96
8.2.160.0140.00020.52
8.2.150.0050.00324.18
8.2.140.0110.00624.66
8.2.130.0070.00026.16
8.2.120.0080.00019.23
8.2.110.0000.00921.05
8.2.100.0040.01117.66
8.2.90.0080.00019.09
8.2.80.0080.00017.97
8.2.70.0080.00017.50
8.2.60.0040.00417.91
8.2.50.0080.00018.10
8.2.40.0090.00020.68
8.2.30.0030.00519.10
8.2.20.0030.00617.61
8.2.10.0040.00417.97
8.2.00.0040.00419.21
8.1.270.0060.00322.07
8.1.260.0040.00426.35
8.1.250.0080.00028.09
8.1.240.0140.00323.97
8.1.230.0110.00020.39
8.1.220.0030.00617.74
8.1.210.0090.00018.77
8.1.200.0000.00917.10
8.1.190.0040.00417.10
8.1.180.0000.00818.10
8.1.170.0080.00018.51
8.1.160.0040.00418.81
8.1.150.0040.00420.00
8.1.140.0040.00417.25
8.1.130.0000.00717.71
8.1.120.0030.00617.23
8.1.110.0040.00417.38
8.1.100.0040.00417.39
8.1.90.0040.00417.31
8.1.80.0030.00317.24
8.1.70.0000.00717.35
8.1.60.0030.00517.42
8.1.50.0040.00417.58
8.1.40.0000.00817.43
8.1.30.0000.00817.50
8.1.20.0000.00817.63
8.1.10.0050.00317.48
8.1.00.0090.00017.49
8.0.300.0040.00418.77
8.0.290.0080.00416.75
8.0.280.0000.00818.43
8.0.270.0030.00317.18
8.0.260.0080.00017.18
8.0.250.0040.00416.88
8.0.240.0000.00716.88
8.0.230.0040.00416.88
8.0.220.0070.00016.78
8.0.210.0000.00716.84
8.0.200.0000.00716.83
8.0.190.0040.00416.86
8.0.180.0000.00716.83
8.0.170.0040.00416.88
8.0.160.0080.00016.86
8.0.150.0070.00016.78
8.0.140.0080.00016.82
8.0.130.0060.00013.27
8.0.120.0050.00216.77
8.0.110.0040.00416.86
8.0.100.0080.00016.77
8.0.90.0040.00416.68
8.0.80.0090.01216.75
8.0.70.0040.00416.85
8.0.60.0030.00516.65
8.0.50.0080.00016.64
8.0.30.0100.00917.14
8.0.20.0120.00717.40
8.0.10.0040.00416.88
8.0.00.0110.00616.65
7.4.330.0000.00615.02
7.4.320.0030.00316.42
7.4.300.0000.00616.47
7.4.290.0000.00716.47
7.4.280.0080.00016.57
7.4.270.0000.00816.42
7.4.260.0000.00716.50
7.4.250.0000.00716.55
7.4.240.0000.00716.58
7.4.230.0040.00416.49
7.4.220.0060.01616.49
7.4.210.0060.00816.56
7.4.200.0000.00716.62
7.4.160.0140.01016.60
7.4.150.0080.01517.40
7.4.140.0090.00917.86
7.4.130.0110.00716.70
7.4.120.0120.00516.51
7.4.110.0070.01116.31
7.4.100.0140.00316.46
7.4.90.0030.01516.65
7.4.80.0110.00719.39
7.4.70.0120.01216.61
7.4.60.0060.01316.46
7.4.50.0000.00816.42
7.4.40.0100.00716.31
7.4.30.0120.00316.50
7.4.00.0090.00915.05
7.3.330.0060.00013.41
7.3.320.0000.00513.21
7.3.310.0030.00316.26
7.3.300.0040.00416.45
7.3.290.0130.00016.43
7.3.280.0100.00816.37
7.3.270.0170.00717.40
7.3.260.0090.01516.44
7.3.250.0120.00516.46
7.3.240.0040.01416.65
7.3.230.0060.01216.39
7.3.210.0190.00016.61
7.3.200.0100.00919.39
7.3.190.0090.00616.68
7.3.180.0120.00416.57
7.3.170.0150.00616.51
7.3.160.0180.00316.43
7.3.120.0120.00314.97
7.2.330.0100.01316.68
7.2.320.0070.01016.48
7.2.310.0060.01616.70
7.2.300.0080.00816.79
7.2.290.0120.00616.78
7.2.60.0030.00916.87
7.2.00.0000.01519.54
7.1.200.0070.00715.85
7.1.100.0030.01018.19
7.1.70.0040.00417.22
7.1.60.0000.02319.70
7.1.50.0150.00916.92
7.1.00.0030.07722.36
7.0.200.0030.00516.72
7.0.140.0070.07022.00
7.0.60.0030.07019.97
7.0.50.0030.08017.90
7.0.40.0030.08320.26
7.0.30.0400.04720.30
7.0.20.0230.03720.21
7.0.10.0330.07320.23
7.0.00.0130.08320.12
5.6.280.0030.07021.14
5.6.210.0000.07320.56
5.6.200.0030.09018.18
5.6.190.0070.05320.77
5.6.180.0470.06720.53
5.6.170.0230.03720.48
5.6.160.0030.08720.50
5.6.150.0130.07018.19
5.6.140.0070.06318.18
5.6.130.0030.04018.18
5.6.120.0100.08021.13
5.6.110.0170.07721.14
5.6.100.0070.08021.00
5.6.90.0130.07720.97
5.6.80.0100.06320.55
5.6.70.0300.07320.55
5.5.350.0100.03720.44
5.5.340.0000.08018.05
5.5.330.0070.04320.41
5.5.320.0200.03320.57
5.5.310.0330.07720.42
5.5.300.0030.06017.96
5.5.290.0030.04017.98
5.5.280.0070.03721.00
5.5.270.0130.06720.66
5.5.260.0070.08020.82
5.5.250.0100.05320.60
5.5.240.0070.07320.21
5.4.450.0030.05319.48
5.4.440.0200.03719.55
5.4.430.0200.03719.52
5.4.420.0230.03319.63
5.4.410.0230.03319.42
5.4.400.0130.04019.15
5.4.390.0200.05019.27
5.4.380.0230.07019.09
5.4.370.0230.04319.06
5.4.360.0230.03019.09
5.4.350.0170.03719.00
5.4.340.0130.04018.98
5.4.320.0130.04018.98
5.4.310.0230.06019.21
5.4.300.0230.07018.95
5.4.290.0200.04719.19
5.4.280.0200.04019.16
5.4.270.0170.03719.22
5.4.260.0170.04319.10
5.4.250.0170.03718.98
5.4.240.0170.03719.01
5.4.230.0130.04018.94
5.4.220.0230.03018.98
5.4.210.0230.03018.97
5.4.200.0200.03318.84
5.4.190.0170.04019.08
5.4.180.0200.03319.13
5.4.170.0170.03718.94
5.4.160.0270.07019.21
5.4.150.0370.03318.82
5.4.140.0200.04316.32
5.4.130.0200.06716.67
5.4.120.0100.04016.52
5.4.110.0170.03716.47
5.4.100.0200.03016.30
5.4.90.0300.06016.59
5.4.80.0200.04716.44
5.4.70.0170.03716.39
5.4.60.0200.03016.34
5.4.50.0170.05716.54
5.4.40.0270.03316.55
5.4.30.0300.05016.24
5.4.20.0270.06716.53
5.4.10.0230.04316.34
5.4.00.0200.03015.78
5.3.290.0170.03714.77
5.3.280.0200.03714.64
5.3.270.0230.04714.64
5.3.260.0130.04314.54
5.3.250.0170.03714.70
5.3.240.0200.03314.53
5.3.230.0270.04014.77
5.3.220.0270.07314.50
5.3.210.0370.06014.57
5.3.200.0230.05714.56
5.3.190.0230.07314.49
5.3.180.0230.06014.67
5.3.170.0300.06314.56
5.3.160.0100.05014.48
5.3.150.0200.04314.59
5.3.140.0200.07014.73
5.3.130.0270.06314.47
5.3.120.0270.04014.48
5.3.110.0230.06314.48
5.3.100.0230.07014.05
5.3.90.0200.06314.27
5.3.80.0130.04314.02
5.3.70.0230.06714.06
5.3.60.0330.06014.05
5.3.50.0200.04313.98
5.3.40.0300.06013.98
5.3.30.0200.04013.84
5.3.20.0300.05713.81
5.3.10.0170.05713.61
5.3.00.0200.03313.78
5.2.170.0130.03711.10
5.2.160.0200.05011.33
5.2.150.0170.03011.26
5.2.140.0200.06011.18
5.2.130.0330.05011.28
5.2.120.0270.05711.04
5.2.110.0270.05711.24
5.2.100.0230.04711.08
5.2.90.0200.04011.04
5.2.80.0230.02311.13
5.2.70.0100.03311.20
5.2.60.0170.03011.03
5.2.50.0170.02711.05
5.2.40.0170.05711.18
5.2.30.0230.03010.90
5.2.20.0130.03710.95
5.2.10.0070.03311.12
5.2.00.0170.02710.75
5.1.60.0200.05310.10
5.1.50.0100.0279.93
5.1.40.0130.0339.90
5.1.30.0130.06010.34
5.1.20.0170.03010.52
5.1.10.0130.02710.19
5.1.00.0300.0409.98
5.0.50.0100.0208.49
5.0.40.0130.0138.68
5.0.30.0100.0478.34
5.0.20.0100.0438.20
5.0.10.0100.0308.10
5.0.00.0100.0378.10
4.4.90.0070.0338.00
4.4.80.0100.0338.00
4.4.70.0070.0238.00
4.4.60.0070.0208.00
4.4.50.0100.0338.00
4.4.40.0100.0308.00
4.4.30.0100.0208.00
4.4.20.0100.0138.00
4.4.10.0100.0178.00
4.4.00.0100.0238.00
4.3.110.0130.0278.00
4.3.100.0100.0138.00
4.3.90.0070.0338.00
4.3.80.0070.0378.00
4.3.70.0100.0238.00
4.3.60.0130.0138.00
4.3.50.0100.0178.00
4.3.40.0030.0408.00
4.3.30.0030.0178.00
4.3.20.0030.0178.00
4.3.10.0030.0308.00
4.3.00.0100.0238.00

preferences:
43.24 ms | 400 KiB | 5 Q