DBookatay Posted November 10, 2006 Share Posted November 10, 2006 How do I trim the amount of word that are returned, like a lot of blogs do, where you'll get the first 50 or 100 or so words from a topic, then have to click on the article to view the full story?My code is this: [code]$text = nl2br(stripslashes($row['cmnts']));[/code]Basically I want to have a snippet of code, followed by "..." Quote Link to comment Share on other sites More sharing options...
joshi_v Posted November 10, 2006 Share Posted November 10, 2006 [code]Function limited_words($var,$limit) #String to break , and the limit of number of words should be in string.{ $word=''; $arr=explode(" ",$var); for($i=0;$i<=$limit;$i++) { $word.=$arr[$i].' '; } return $word; # return string with limited number of words.}[/code]Hop it will be useful for you!RegardsJoshi. Quote Link to comment Share on other sites More sharing options...
Monkeymatt Posted November 10, 2006 Share Posted November 10, 2006 Here's an example for 50 words:[code]<?php$temp=explode(" ", stripslashes($row['cmnts']), 51); // 51 is 50 words +1 (extra words)array_pop($temp); // remove extra words from end$text=nl2br(implode(" ", $temp)."..."); // add spaces back in and add '...'?>[/code]Monkeymatt Quote Link to comment Share on other sites More sharing options...
DBookatay Posted November 10, 2006 Author Share Posted November 10, 2006 [quote author=Monkeymatt link=topic=114496.msg465861#msg465861 date=1163138876]Here's an example for 50 words:[code]<?php$temp=explode(" ", stripslashes($row['cmnts']), 51); // 51 is 50 words +1 (extra words)array_pop($temp); // remove extra words from end$text=nl2br(implode(" ", $temp)."..."); // add spaces back in and add '...'?>[/code]Monkeymatt[/quote]Thank you Monkey, worked like a charm... (No offense josh, this one was easier for a noobie like me to understand) Quote Link to comment 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.