×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Jing Tai Piao
Added: Feb 4, 2013 3:01 AM
Views: 12
Tags: no tags
  1. function truncate($string, $length, $stopanywhere = false) {
  2.         //truncates a string to a certain char length, stopping on a word if not specified otherwise.
  3.         if (strlen($string) > $length) {
  4.             //limit hit!
  5.             $string = substr($string, 0, ($length - 3));
  6.             if ($stopanywhere) {
  7.                 //stop anywhere
  8.                 $string .= '...';
  9.             } else {
  10.                 //stop on a word.
  11.                 $string = substr($string, 0, strrpos($string, ' ')) . '...';
  12.             }
  13.         }
  14.         return $string;
  15.     }