3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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).
Note: hhvm uses a JIT which only kicks in after a few runs. Since we run scripts only once, hhvm always runs slower.

VersionSystem time (s)User time (s)Memory (MiB)
7.2.60.0140.00016.84
7.2.00.0000.01519.42
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