3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
Output for 5.3.0 - 7.1.7
object(Zefram_Filter_StringTruncate)#1 (4) { ["_length":protected]=> int(16) ["_ending":protected]=> string(3) "..." ["_breakWords":protected]=> bool(false) ["_charset":protected]=> string(5) "utf-8" }
Output for 5.0.0 - 5.2.17
object(Zefram_Filter_StringTruncate)#1 (4) { ["_length:protected"]=> int(16) ["_ending:protected"]=> string(3) "..." ["_breakWords:protected"]=> bool(false) ["_charset:protected"]=> string(5) "utf-8" }
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/GAHuu on line 8
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 T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/GAHuu on line 8
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/GAHuu on line 8
Process exited with code 255.