Forum Moderators: coopster
I have a News articles table with a headline and text... I query the database and return both, but I only want to display say the first 30 words of the article text and append it with "... MORE" as a link to the complete article.
The last part is easy, it's the limiting of the number of words displayed that has me stumped.
Russ
$query = "SELECT [url=http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring]SUBSTR[/url](text,0,30) AS short_text FROM table";
I quickly read over your post the first time and did not see that you wanted the first 30 words. To get the words you are going to need to count the spaces; if you have 29 spaces you have 30 words. This, however, will require more work than I personally think is needed to just display a preview of an article. So instead of using 30 characters like I have in my answer above, you might want to use something like 200 characters. This should give a little better preview of the text.
Also, I seem to have forgotten my manners: Welcome to WebmasterWorld! :)
<?phpfunction strtrim($str, $maxlen=100, $elli=NULL, $maxoverflow=15) {
global $CONF;
if (strlen($str) > $maxlen) {
if ($CONF["BODY_TRIM_METHOD_STRLEN"]) {
return substr($str, 0, $maxlen);
}
$output = NULL;
$body = explode(" ", $str);
$body_count = count($body);
$i=0;
do {
$output .= $body[$i]." ";
$thisLen = strlen($output);
$cycle = ($thisLen < $maxlen && $i < $body_count-1 && ($thisLen+strlen($body[$i+1])) < $maxlen+$maxoverflow?true:false);
$i++;
} while ($cycle);
return $output.$elli;
}
else return $str;
}?>