quote: I assume it goes somewhere in the $getnews query?
Yep. As a general rule you should never do "SELECT *", but always specify the precise columns you want.
Specifically, the query should initially read like this:
SQL code: SELECT id
, title
, date
, ip_address
, news
FROM news
[etc]
And you want to update it to be:
SQL code: SELECT id
, title
, date
, ip_address
, LEFT(news,300) AS news_brief
FROM news
[etc]
That will get the first three hundred characters, but may well cut off in the middle of a word, which you don't want.
So, in your big echo, instead of just $news, you want this:
PHP code: if (strlen($news_brief) < 300)
{
// less than 300 chars, so not cut off in middle of word; show full thing.
echo $news_brief;
}
else
{
// 300 or more characters, so remove any part-words from end of string by having it end in whitespace.
preg_match('/(\S+\s*?){1,50}/', $news_brief, $nb_fullwords);
echo $nb_fullwords[0] . "...";
}
Obviously you'll need to end the initial echo for that and start a new one.
You might notice I've modified the main regex bit from (\w+\W*?) to (\S+\s*?) - that's because I just realised the first one could cut URLs up, whilst the second one shouldn't.
(\w = any word character, \W means non-word character, whereas \s and \S are whitespace and non-whitespace respectively. \W would (unwantedly) match punctuaction, whilst \s wont.)
Oh, and I can't resist pointing out that you'd be much better off with simpler HTML than you have there, but I wont go on about it unless you want me to. ;)
|