3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Zefram_Filter_StringTruncate implements Zend_Filter_Interface { /** * @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; } }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught Error: Interface "Zend_Filter_Interface" not found in /in/UDEYo:3 Stack trace: #0 {main} thrown in /in/UDEYo on line 3
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught Error: Interface "Zend_Filter_Interface" not found in /in/UDEYo:3 Stack trace: #0 {main} thrown in /in/UDEYo on line 3
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Interface 'Zend_Filter_Interface' not found in /in/UDEYo:3 Stack trace: #0 {main} thrown in /in/UDEYo on line 3
Process exited with code 255.
Output for 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33
Fatal error: Interface 'Zend_Filter_Interface' not found in /in/UDEYo on line 3
Process exited with code 255.
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Interface 'Zend_Filter_Interface' not found in /in/UDEYo on line 4
Process exited with code 255.
Output for 5.0.0 - 5.0.4
Fatal error: Class 'Zend_Filter_Interface' not found in /in/UDEYo on line 3
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/UDEYo on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/UDEYo on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/UDEYo on line 3
Process exited with code 255.

preferences:
314.99 ms | 401 KiB | 459 Q