dennismonsewicz Posted August 5, 2008 Share Posted August 5, 2008 how would I go about taking text like out of a DB and truncate it after a set amount of characters? After 80 characters i would like to place a "read more" statement. Link to comment https://forums.phpfreaks.com/topic/118292-solved-truncating-after-a-certain-amount-of-characters/ Share on other sites More sharing options...
lemmin Posted August 5, 2008 Share Posted August 5, 2008 I just did one of those not too long ago: <script language=\"JScript\"> function showMore(e, text) { e.vTemp = e.innerHTML; e.innerHTML = text + \" <a href='Javascript://' STYLE='font-size:10px' onclick='this.parentElement.innerHTML=this.parentElement.vTemp'>(less)</a>\"; } </script> if (strlen($value) > 80) echo "<b>$field:</b><br><div STYLE=\"margin-left:40px;font-style:italic;width:300px\">". substr($value, 0, 77) ."...<a href=\"Javascript://;\" STYLE=\"font-size:10px\" onclick=\"showMore(this.parentElement, '<i>".preg_replace("[']", "\'", $value)."</i>');\">(more)</a></div>"; else echo "<b>$field:</b><br><div STYLE=\"margin-left:40px\">". $value ."</div><br>"; I think just this part answers your question, though: substr($value, 0, 77); Link to comment https://forums.phpfreaks.com/topic/118292-solved-truncating-after-a-certain-amount-of-characters/#findComment-608746 Share on other sites More sharing options...
DarkWater Posted August 5, 2008 Share Posted August 5, 2008 Assuming $text is your text from the DB: $text = substr($text, 0, 80) . "..."; $text .= sprintf('<a href="articles.php?id=%d">Read More</a>', $id); Assuming articles.php is your Read More page and $id is your text's ID. >_> Link to comment https://forums.phpfreaks.com/topic/118292-solved-truncating-after-a-certain-amount-of-characters/#findComment-608747 Share on other sites More sharing options...
dennismonsewicz Posted August 5, 2008 Author Share Posted August 5, 2008 thanks bud! Problem Solv... ed! Link to comment https://forums.phpfreaks.com/topic/118292-solved-truncating-after-a-certain-amount-of-characters/#findComment-608760 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.