Close Panel
Hello Guest! | Log In


lheader.gif rheader.gif
tgcontentlogo.gif
tgcontentlogob.gif

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) &gt; $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);
?>

2 Comments

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

lhfooter.gif
Valid XHTML 1.0 | Valid CSS | Load Time: 0.397776 sec(s)
rfooter.gif

Strict Standards: is_a(): Deprecated. Please use the instanceof operator in /home/tg007net/public_html/blog/wp-includes/script-loader.php on line 607