Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static public function smartCut( $string, $limit )
- {
- if ( $limit < 1 )
- {
- return '';
- }
- if ( mb_strlen($string) <= $limit )
- {
- return $string;
- }
- // Ищем пробел
- for ( $i = $limit; $i >= 0; $i-- )
- {
- if ( mb_substr($string, $i, 1) == ' ' )
- {
- $result = mb_substr($string, 0, $i);
- // Счищаем с конца нетекстовые символы
- preg_match('~^.*[а-яёa-z0-9]~uis', $result, $matches);
- return $matches[0] . '…';
- }
- }
- // Если не нашли - просто рубим
- return mb_substr($string, 0, $limit - 1) . '…';
- }
Add Comment
Please, Sign In to add comment