Skip to content

Instantly share code, notes, and snippets.

@5iDS
Created May 23, 2013 20:37
Show Gist options
  • Save 5iDS/5639245 to your computer and use it in GitHub Desktop.
Save 5iDS/5639245 to your computer and use it in GitHub Desktop.
Counting String Characters
function smartShorten($charset='utf-8', $str, $maxlength = 140, $max_cut_len = 10) {
$len = iconv_strlen($str, $charset);
if ($len > $maxlength) {
$str = iconv_substr($str, 0, $maxlength, $charset);
$prev_space_pos = iconv_strrpos($str, ' ', $charset);
if ( ($maxlength-$prev_space_pos) < $max_cut_len) {
$str = iconv_substr($str, 0, $prev_space_pos, $charset);
}
$str .= '...';
}
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment