Trim a string by length without cutting off words

August 21, 2013

function trim_string($string,$length,$intact = true,$filler = ' ...'){
if(strlen($string) < $length){
 return $string;
 }else{
 if($intact){
 while (substr ($string , $length ,1) != " ") {
 $length--;
 }
 }
 $new_string = substr($string,0,$length) . $filler;
 return $new_string;
 }

Tags: ,