Guest User

Untitled

a guest
Oct 18th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.63 KB | None | 0 0
  1.     static public function smartCut( $string, $limit )
  2.     {
  3.         if ( $limit < 1 )
  4.         {
  5.             return '';
  6.         }
  7.  
  8.         if ( mb_strlen($string) <= $limit )
  9.         {
  10.             return $string;
  11.         }
  12.  
  13.         // Ищем пробел
  14.  
  15.         for ( $i = $limit; $i >= 0; $i-- )
  16.         {
  17.             if ( mb_substr($string, $i, 1) == ' ' )
  18.             {
  19.                 $result = mb_substr($string, 0, $i);
  20.  
  21.                 // Счищаем с конца нетекстовые символы
  22.  
  23.                 preg_match('~^.*[а-яёa-z0-9]~uis', $result, $matches);
  24.  
  25.                 return $matches[0] . '…';
  26.             }
  27.         }
  28.  
  29.         // Если не нашли - просто рубим
  30.  
  31.         return mb_substr($string, 0, $limit - 1) . '…';
  32.     }
Add Comment
Please, Sign In to add comment