piblondin Posted February 5, 2010 Share Posted February 5, 2010 Hi everyone, I am trying to truncate text from stories that I'm pulling from a database. However, the code below doesn't seem to be truncating them at all. Here is the function: function myTruncate2($string, $limit, $break=" ", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string; $string = substr($string, 0, $limit); if(false !== ($breakpoint = strrpos($string, $break))) { $string = substr($string, 0, $breakpoint); } return $string . $pad; } Here's the code that is generating the HTML: <?php $summary = blurb(1); $shortdesc = myTruncate2($summary, 10); echo "<p>$shortdesc</p>"; ?> This is displaying full stories, the same as I would get with blurb(1) or echo $summary blurb() is a function that returns the summary of our stories, so that blurb(4) would return the text of the story with key = 4 in the MySQL database. I noticed that when I try strlen($shortdesc), it returns 0. Any suggestions? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/191104-truncating-text-returned-by-function/ Share on other sites More sharing options...
premiso Posted February 6, 2010 Share Posted February 6, 2010 Just tried this: <?php $summary = "some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. some tideous summary that I just decided to copy. x"; $trunc = myTruncate2($summary, 10); echo $trunc; /* Outputs: some tideo... */ function myTruncate2($string, $limit, $break="", $pad="...") { // return with no change if string is shorter than $limit if(strlen($string) <= $limit) return $string; $string = substr($string, 0, $limit); if(false !== ($breakpoint = strrpos($string, $break))) { $string = substr($string, 0, $breakpoint); } return $string . $pad; } ?> And it worked as expected. Perhaps your blurb function is echoing the content instead of returning it as a string. To capture the echo look into ob_start and ob_get_clean to add around the $summary = portion. Or, the more preferred method, modify the blurb function to return the summary and not echo it. Quote Link to comment https://forums.phpfreaks.com/topic/191104-truncating-text-returned-by-function/#findComment-1007714 Share on other sites More sharing options...
piblondin Posted February 8, 2010 Author Share Posted February 8, 2010 That is exactly the issue. Going to try to figure this out now. The following is the blurb() function from the functions.php file that we're including: function blurb($storyId) { $query = "SELECT blurb FROM `testdate` WHERE story_id = $storyId"; $result = mysql_query($query); if ($result) { $row = mysql_fetch_assoc($result); echo $row['blurb']; } Quote Link to comment https://forums.phpfreaks.com/topic/191104-truncating-text-returned-by-function/#findComment-1009025 Share on other sites More sharing options...
wildteen88 Posted February 8, 2010 Share Posted February 8, 2010 Use return not echo http://www.php.net/manual/en/functions.returning-values.php Quote Link to comment https://forums.phpfreaks.com/topic/191104-truncating-text-returned-by-function/#findComment-1009027 Share on other sites More sharing options...
piblondin Posted February 8, 2010 Author Share Posted February 8, 2010 Yup, got it working! Thanks, guys! Quote Link to comment https://forums.phpfreaks.com/topic/191104-truncating-text-returned-by-function/#findComment-1009039 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.