PDA

View Full Version : how to limit the number of words returned


Sean S
01-09-2006, 04:43 PM
Hi everyone,

I'm working on a little module that lists the latest 5 blog entries of each user. So far I have got the coding part right and it works the way I want to, but there is one thing I don't know how to do and need your help.

I would like to only show the first 30 words of the blog entry on the top list, and I don't know how to limit the mysql query to do that. Could anyone tell me how I can achieve that? thank you.

sabret00the
01-10-2006, 10:57 AM
substr is the easiest method, but that's characters, you want to limit words then you're gonna have to make a function


function return_words($string)
{
global vbulletin;

$word = 0;

$string = explode(' ', $string);

foreach ($string AS $key => $value)
{
if ($word == $vbulletin->option['blog_words_returned'])
{
break;
}
else
{
$string .= " " . $value;
}

$word++;
}

return trim($string);
}


something like that ;)

Sean S
01-11-2006, 06:32 PM
ah I got it working with the substr, thanks you very much :)

Andrew
01-11-2006, 07:48 PM
Just out of curiosity how would you limit it to X characters ?

filburt1
01-12-2006, 01:06 AM
<a href="http://www.php.net/substr" target="_blank">http://www.php.net/substr</a>

Although it's better to limit it in the query.

Sean S
01-12-2006, 11:11 PM
basically here is what I did and it worked for me, first I called the variable in a string, and then I created another variable that outputs the substr function using that string that I created,

$string = "$blogentry";
$rest = substr("$string", 0, 50) . "...";

echo "$rest"

basically that 0 is where you want the counting to start, and 50 is the amount that you want to limit. So for me, the counting is going to start from the first (0) character and it's going to stop after the 50th char, and after that it is going to place the "...". Now I'm new to this myself and this way worked for me, someone else here could probably make it work easier than that, hope that helps :)