3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Zefram_Filter_StringTruncate { /** * @var int */ protected $_length = 80; /** * @var string */ protected $_ending = '...'; /** * @var bool */ protected $_breakWords = false; /** * @var string */ protected $_charset = 'utf-8'; /** * @param array|Traversable $options OPTIONAL */ public function __construct($options = null) { if ($options) { foreach ($options as $key => $value) { $method = 'set' . $key; if (method_exists($this, $method)) { $this->$method($value); } } } } /** * @param int $length * @return Zefram_Filter_StringTruncate */ public function setLength($length) { $this->_length = (int) $length; return $this; } /** * @return int */ public function getLength() { return $this->_length; } /** * @param string $ending * @return Zefram_Filter_StringTruncate */ public function setEnding($ending) { $this->_ending = (string) $ending; return $this; } /** * @return string */ public function getEnding($ending) { return $this->_ending; } /** * @param bool $breakWords * @return Zefram_Filter_StringTruncate */ public function setBreakWords($breakWords) { $this->_breakWords = (bool) $breakWords; return $this; } /** * @return bool */ public function getBreakWords() { return $this->_breakWords; } /** * @param string $charset * @return Zefram_Filter_StringTruncate */ public function setCharset($charset) { $this->_charset = (string) $charset; return $this; } /** * @return string */ public function getCharset() { return $this->_charset; } /** * @param string $value * @return string */ public function filter($value) { return self::stringTruncate( $value, $this->getLength(), $this->getEnding(), $this->getBreakWords(), $this->getCharset() ); } /** * @param string $string * @param int $length * @param string $more * @param bool $breakWords * @param string $enc * @return string */ public static function stringTruncate($string, $length = 80, $more = '...', $breakWords = false, $enc = 'utf-8') { $length = (int) $length; if ($length <= 0) { return ''; } // trim input string $string = (string) $string; $string = preg_replace('/(^\s+)|(\s+$)/u', '', $string); if (mb_strlen($string, $enc) > $length) { // make room for postfix $length -= min($length, mb_strlen($more, $enc)); // do not break words, truncate string at last whitespace // found between 0 and $length index if (!$breakWords) { $string = preg_replace('/\s+(\S+)?$/u', '', mb_substr($string, 0, $length + 1)); // One of two scenarios took place: // - dangling word preceded by whitespace was truncated, or // - string is intact, as it consists of a single word // length+1 jest na wypadek, gdyby ostatni wyraz konczyl sie na granicznej // pozycji. W przeciwnym razie nie byłby uwzględniony w wyniku. Przykładowo: // $string = 'a_bc_'; // obcinamy do 4 znakow -> bez length+1 dostajemy 'a', z length+1 dostajemy 'a_bc' } return mb_substr($string, 0, $length, $enc) . $more; } return $string; } } $filter = new Zefram_Filter_StringTruncate(array('length' => 16)); var_dump($filter);

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.0090.00618.56
8.3.50.0180.00322.18
8.3.40.0040.01118.84
8.3.30.0120.00319.17
8.3.20.0060.00321.92
8.3.10.0050.00322.13
8.3.00.0110.00422.43
8.2.180.0150.00618.14
8.2.170.0140.00422.96
8.2.160.0120.00319.08
8.2.150.0040.00424.18
8.2.140.0110.00024.66
8.2.130.0000.00726.16
8.2.120.0060.00317.63
8.2.110.0040.00422.23
8.2.100.0110.00017.72
8.2.90.0000.00719.13
8.2.80.0000.00918.04
8.2.70.0000.01217.38
8.2.60.0030.00617.93
8.2.50.0000.01118.07
8.2.40.0000.00718.09
8.2.30.0050.00318.19
8.2.20.0060.00317.59
8.2.10.0040.00419.57
8.2.00.0030.00617.56
8.1.280.0070.01025.92
8.1.270.0080.00022.18
8.1.260.0050.00326.35
8.1.250.0070.00028.09
8.1.240.0030.00623.82
8.1.230.0070.00419.15
8.1.220.0070.00417.74
8.1.210.0040.00418.77
8.1.200.0030.00717.47
8.1.190.0050.00317.43
8.1.180.0040.00418.10
8.1.170.0030.00518.50
8.1.160.0000.01122.04
8.1.150.0040.00418.66
8.1.140.0040.00417.46
8.1.130.0060.00317.77
8.1.120.0000.00717.52
8.1.110.0050.00317.37
8.1.100.0000.00717.36
8.1.90.0000.00717.48
8.1.80.0040.00417.39
8.1.70.0000.00717.36
8.1.60.0070.00417.64
8.1.50.0000.00817.55
8.1.40.0000.00817.49
8.1.30.0080.00017.66
8.1.20.0000.00817.59
8.1.10.0030.00617.58
8.1.00.0000.00817.45
8.0.300.0000.00718.77
8.0.290.0040.00417.05
8.0.280.0070.00018.37
8.0.270.0030.00317.29
8.0.260.0030.00316.76
8.0.250.0030.00316.95
8.0.240.0100.00016.96
8.0.230.0050.00217.00
8.0.220.0080.00016.95
8.0.210.0040.00416.93
8.0.200.0000.00817.01
8.0.190.0060.00316.89
8.0.180.0000.00716.83
8.0.170.0040.00416.93
8.0.160.0040.00416.85
8.0.150.0040.00416.87
8.0.140.0040.00316.91
8.0.130.0030.00313.31
8.0.120.0040.00416.93
8.0.110.0000.00816.96
8.0.100.0040.00416.78
8.0.90.0000.00716.72
8.0.80.0130.00316.89
8.0.70.0060.00316.80
8.0.60.0000.00716.90
8.0.50.0000.00716.72
8.0.30.0100.01317.17
8.0.20.0090.00917.40
8.0.10.0040.00416.92
8.0.00.0080.01216.70
7.4.330.0050.00015.00
7.4.320.0030.00316.66
7.4.300.0030.00316.64
7.4.290.0020.00516.68
7.4.280.0070.00016.53
7.4.270.0000.00716.52
7.4.260.0000.00716.52
7.4.250.0000.00716.43
7.4.240.0040.00316.55
7.4.230.0000.00716.59
7.4.220.0090.00916.66
7.4.210.0050.01316.57
7.4.200.0050.00216.73
7.4.190.0000.00716.70
7.4.160.0120.00416.41
7.4.150.0120.00617.40
7.4.140.0150.00517.86
7.4.130.0120.01216.60
7.4.120.0110.01016.64
7.4.110.0100.00716.43
7.4.100.0130.01016.51
7.4.90.0030.01416.58
7.4.80.0030.01719.39
7.4.70.0100.00716.40
7.4.60.0130.00316.34
7.4.50.0040.00416.48
7.4.40.0030.01122.77
7.4.30.0060.01516.66
7.4.00.0000.01414.92
7.3.330.0060.00013.34
7.3.320.0000.00613.29
7.3.310.0050.00216.19
7.3.300.0030.00316.39
7.3.290.0140.00316.36
7.3.280.0080.01016.34
7.3.270.0120.00617.40
7.3.260.0130.01316.65
7.3.250.0120.01116.61
7.3.240.0100.00716.41
7.3.230.0170.00016.68
7.3.210.0170.00016.37
7.3.200.0110.00919.39
7.3.190.0090.01316.46
7.3.180.0120.00916.31
7.3.170.0100.01316.28
7.3.160.0110.00716.38
7.3.120.0120.00315.09
7.3.10.0140.00516.45
7.3.00.0110.00316.34
7.2.330.0080.00916.53
7.2.320.0120.00616.70
7.2.310.0100.00716.53
7.2.300.0030.01316.62
7.2.290.0090.00916.55
7.2.130.0030.00716.78
7.2.120.0110.00616.77
7.2.110.0060.00616.55
7.2.100.0040.00817.03
7.2.90.0050.00516.99
7.2.80.0030.01016.39
7.2.70.0030.00916.54
7.2.60.0040.00716.77
7.2.50.0100.00316.70
7.2.40.0060.00316.66
7.2.30.0060.00316.57
7.2.20.0080.00416.89
7.2.10.0080.00416.60
7.2.00.0000.01116.73
7.1.250.0060.00915.63
7.1.70.0000.00917.24
7.1.60.0090.01519.82
7.1.50.0040.01817.04
7.1.00.0070.07022.31
7.0.200.0040.00716.64
7.0.140.0130.06321.95
7.0.60.0070.08019.96
7.0.50.0130.04317.91
7.0.40.0100.04020.24
7.0.30.0300.08320.08
7.0.20.0200.03720.25
7.0.10.0100.06720.30
7.0.00.0100.08020.16
5.6.280.0070.07020.93
5.6.210.0030.08020.77
5.6.200.0070.08718.28
5.6.190.0200.07320.77
5.6.180.0200.07720.46
5.6.170.0130.04320.50
5.6.160.0070.05020.51
5.6.150.0030.04018.14
5.6.140.0000.04318.28
5.6.130.0030.08318.29
5.6.120.0030.05321.02
5.6.110.0130.07721.17
5.6.100.0100.08021.15
5.6.90.0070.07721.00
5.6.80.0100.05020.40
5.6.70.3370.03320.41
5.5.350.0300.06720.32
5.5.340.0100.07318.07
5.5.330.0100.08020.40
5.5.320.0370.07020.34
5.5.310.0230.07720.25
5.5.300.0100.08017.95
5.5.290.0100.04017.96
5.5.280.0030.05720.82
5.5.270.0070.05320.82
5.5.260.0100.07720.89
5.5.250.0030.04020.71
5.5.240.0330.04320.38
5.4.450.0330.06019.59
5.4.440.0630.05319.59
5.4.430.0070.05719.52
5.4.420.0100.06319.51
5.4.410.0230.04319.06
5.4.400.0130.05018.82
5.4.390.0100.05318.79
5.4.380.0100.05318.79
5.4.370.0230.04718.84
5.4.360.0200.05018.63
5.4.350.0270.04718.75
5.4.340.0170.05718.58
5.4.320.0060.03512.52
5.4.310.0060.04112.52
5.4.300.0030.04212.52
5.4.290.0040.04112.52
5.4.280.0040.03712.41
5.4.270.0070.05718.98
5.4.260.0070.05718.92
5.4.250.0100.06019.23
5.4.240.0030.07019.23
5.4.230.0130.06718.99
5.4.220.0330.06019.14
5.4.210.0100.06319.23
5.4.200.0130.06719.14
5.4.190.0130.06018.91
5.4.180.0070.05718.96
5.4.170.0130.06718.82
5.4.160.0170.05718.83
5.4.150.0130.06718.92
5.4.140.0130.05016.66
5.4.130.0170.06316.62
5.4.120.0230.07716.35
5.4.110.0100.07016.64
5.4.100.0130.04716.52
5.4.90.0030.05316.74
5.4.80.0070.07316.53
5.4.70.0100.05316.52
5.4.60.0070.07016.49
5.4.50.0130.06716.65
5.4.40.0100.05016.45
5.4.30.0230.06016.39
5.4.20.0170.05716.64
5.4.10.0170.07016.54
5.4.00.0100.07015.83
5.3.290.0080.03912.80
5.3.280.0100.07314.61
5.3.270.0030.06314.68
5.3.260.0030.05714.91
5.3.250.0170.05714.68
5.3.240.0130.05314.62
5.3.230.0170.05714.90
5.3.220.0170.05014.65
5.3.210.0100.06014.47
5.3.200.0000.06014.85
5.3.190.0130.06014.76
5.3.180.0130.04714.87
5.3.170.0170.07014.87
5.3.160.0200.04314.77
5.3.150.0170.06014.79
5.3.140.0230.05314.76
5.3.130.0070.08314.85
5.3.120.0170.06714.84
5.3.110.0130.06714.84
5.3.100.0000.06714.35
5.3.90.0200.05714.21
5.3.80.0070.06714.09
5.3.70.0130.05714.04
5.3.60.0100.06314.21
5.3.50.0170.05314.04
5.3.40.0130.04714.10
5.3.30.0170.06014.21
5.3.20.0100.07013.89
5.3.10.0100.05713.93
5.3.00.0200.04713.95
5.2.170.0100.05311.17
5.2.160.0230.04311.48
5.2.150.0100.05311.37
5.2.140.0070.05311.22
5.2.130.0200.04011.39
5.2.120.0170.04711.33
5.2.110.0170.04711.01
5.2.100.0170.04011.18
5.2.90.0070.04011.32
5.2.80.0100.04711.34
5.2.70.0070.04011.24
5.2.60.0070.05311.38
5.2.50.0070.05311.09
5.2.40.0100.03711.07
5.2.30.0130.05311.18
5.2.20.0070.05711.26
5.2.10.0070.04711.00
5.2.00.0070.05010.75
5.1.60.0100.03710.32
5.1.50.0100.03710.34
5.1.40.0030.05010.30
5.1.30.0070.04310.55
5.1.20.0030.04310.59
5.1.10.0100.04710.08
5.1.00.0100.03710.20
5.0.50.0000.0438.74
5.0.40.0100.0308.55
5.0.30.0000.0508.45
5.0.20.0030.0378.44
5.0.10.0100.0238.27
5.0.00.0100.0438.51
4.4.90.0070.0237.21
4.4.80.0030.0237.21
4.4.70.0000.0307.21
4.4.60.0070.0277.21
4.4.50.0070.0237.21
4.4.40.0030.0337.21
4.4.30.0000.0337.21
4.4.20.0030.0237.21
4.4.10.0100.0277.21
4.4.00.0000.0477.21
4.3.110.0100.0237.21
4.3.100.0100.0207.21
4.3.90.0000.0237.21
4.3.80.0030.0337.21
4.3.70.0030.0207.21
4.3.60.0070.0177.21
4.3.50.0070.0207.21
4.3.40.0070.0407.21
4.3.30.0000.0307.21
4.3.20.0070.0207.21
4.3.10.0000.0237.21
4.3.00.0000.0237.16

preferences:
41.87 ms | 401 KiB | 5 Q