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>


26 user(s) currently browsing site (0 members, 26 guests, 0 anonymous)
No Members Logged In


Warning: Cannot modify header information - headers already sent by (output started at /home/tg007net/public_html/blog/wp-content/themes/tg007/header.php:3) in /home/tg007net/public_html/scriptsocket/roomlist.php on line 2
12 Users in IRCommunity
inføвøт, err0r, «Chãin™», ♥fiestýβสbỳ♥, båbyġurł, Enya, Spike1506, Timeless, «·Ħåĺø·», ﴾Ɣ﴿ⓨⓢⓣⓘⓒ★§ⓟⓔⓛⓛ, danger , [☠] Ƙʀʏᴘтιӿ [☠]



lhfooter.gif
Valid XHTML 1.0 | Valid CSS | Load Time: 0.648079 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 725

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