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 "..."
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

[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)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.