3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 5.0.5 - 5.2.17, 7.0.0 - 7.1.0
Fatal error: Interface 'Zend_Filter_Interface' not found in /in/UDEYo on line 3
Process exited with code 255.
Output for hhvm-3.10.0 - 3.12.0
Fatal error: Undefined interface: Zend_Filter_Interface in /in/UDEYo on line 3
Process exited with code 255.
Output for 5.3.0 - 5.6.28
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.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.