Advertisement
wzline

Truncate Text

Aug 12th, 2019
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.41 KB | None | 0 0
  1. function TruncateTextW(string $text, int $length) : string
  2. {
  3.     $result = '';
  4.     if (strlen($text) <= $length)
  5.     {
  6.         $result = $text;
  7.     }
  8.     else
  9.     {
  10.         $result = substr($text, 0, $length);
  11.         $end = substr($text, $length);
  12.         $m = null;
  13.         if (preg_match('/([.,\s!?\r:;])/', $end, $m))
  14.         {
  15.             $firstSeparator = strpos($end, $m[1]);
  16.             $result .= substr($end, 0, $firstSeparator).'...';
  17.         }
  18.     }
  19.     return $result;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement