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; }


Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>

More information about formatting options