Created
May 23, 2013 20:37
-
-
Save 5iDS/5639245 to your computer and use it in GitHub Desktop.
Counting String Characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode 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