Skip to content

Instantly share code, notes, and snippets.

@mindscms
Created March 16, 2021 00:25
Show Gist options
  • Save mindscms/b36cfa154c698dd092389572f003f2f3 to your computer and use it in GitHub Desktop.
Save mindscms/b36cfa154c698dd092389572f003f2f3 to your computer and use it in GitHub Desktop.
function word_limiter($str, $limit = 100, $end_char = '…'){
$str2 = strip_tags($str);
//$str2 = $str;
if (trim($str2) == ''){
return $str2;
}
preg_match('/^\s*+(?:\S++\s*+){1,'.(int) $limit.'}/', $str2, $matches);
if (strlen($str2) == strlen($matches[0])){
$end_char = '';
}
return rtrim($matches[0]).$end_char;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment