Showing posts with label truncate. Show all posts
Showing posts with label truncate. Show all posts

Thursday, March 12, 2009

Truncating the text

In many places the description or text needs to be truncated. Following function can be used to truncate the text in such a way that the words are not half chopped up.

Argument List:
  1. $mystring: The text
  2. $limit: length of the string upto which string is to be truncate.
  3. $pad: The string which shows continuation. Default it takes 3 dots (...) .


function truncate_text($mystring, $limit = 153,$pad ="..."){
if(strlen($mystring) < $limit)
return $mystring;
$rstring = substr($mystring,0,$limit);
$no_of_chars = strrpos($rstring," ");
if($no_of_chars < $limit)
$rstring = substr($mystring,0,$no_of_chars).$pad;
return $rstring;
}