Jump to content

Returning only a certian number of words from a row.


DBookatay

Recommended Posts

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 "..."
[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!

Regards
Joshi.
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 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)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.