How to truncate text / cut off text at certain length
I ran across this php script while searching for ways to repair my truncation script. I was having a bit of trouble with break tags being cut off and messing up the site validation. I used the idea from this script to fix that. Hope it helps others.
<?php function truncate($text,$numb) {
$text = html_entity_decode($text, ENT_QUOTES);
if (strlen($text) > $numb) {
$text = substr($text, 0, $numb);
$text = substr($text,0,strrpos($text," "));
//This strips the full stop:
if ((substr($text, -1)) == ".") {
$text = substr($text,0,(strrpos($text,".")));
}
$etc = "…";
$text = $text.$etc;
}
$text = htmlentities($text, ENT_QUOTES);
return $text;
}
//Call function
truncate($text, 75);
?>
$text = html_entity_decode($text, ENT_QUOTES);
if (strlen($text) > $numb) {
$text = substr($text, 0, $numb);
$text = substr($text,0,strrpos($text," "));
//This strips the full stop:
if ((substr($text, -1)) == ".") {
$text = substr($text,0,(strrpos($text,".")));
}
$etc = "…";
$text = $text.$etc;
}
$text = htmlentities($text, ENT_QUOTES);
return $text;
}
//Call function
truncate($text, 75);
?>






Thank You, ive been looking for code like this.
I also found this scripts solving the same problem:
http://www.the-art-of-web.com/php/truncate/
Thanks,
Doron Tamir