bishawjit-das
12/10/2016 - 4:53 AM

truncate a string only at a whitespace

truncate a string only at a whitespace

//truncate a string only at a whitespace
function truncate($text, $length) {
   $length = abs((int)$length);
   if(strlen($text) > $length) {
      $text = preg_replace("/^(.{1,$length})(\s.*|$)/s", '\\1...', $text);
   }
   return($text);
}