Tuesday, September 4, 2012

Function for shorten long text without partial words


 function shortenText($text, $length = '')    {
// $normal_text = strip_tags(html_entity_decode($text, ENT_QUOTES));
$max = $length;
$normal_text = strip_tags($text, ENT_QUOTES);
$tot_len = strlen($normal_text);
if(!$length)$length = $tot_len;
if($length < $tot_len)   {
while(substr(strip_tags($text, ENT_QUOTES), $max, 1) != ' ' && $max > 0 )  {
$max--;
}
if($max == 0)$max = $length;
}
$output = substr($normal_text, 0, $max);
if($length < $tot_len)$output .= '<span dir="ltr">...<span>';
return $output;
}

No comments:

Post a Comment