Re: Get just first 200 characters of field?
| From: | fabrizio dot ermini at sysdat dot it | Date: | Thu, 19 Oct 2000 08:48:04 +0000 |
| Subject: | Re: Get just first 200 characters of field? | ||
| References: | 1 | Groups: | php.db php.general |
| Request: | Send a blank email to php-general+get-21124@lists.php.net to get a copy of this message | ||
On 18 Oct 2000, at 9:00, James Montz wrote:
> Im working on my own news managment system for my web site, and I would like
> to be able to post on the main page the article header, and just the first
> 200 or so characters of the actual content of the article.
>
James, for the same goal I've made a little function that cuts the
first XX words, and postpone suspension dots ("...") instead than
cutting them at middle.
Given that you've already SELECTed your piece of news and saved
it into the variable "$newsbody", here it is:
function subtitle($newsbody)
{
$words=explode(" ",$newsbody);
$line="";
for($i=0;$i<XX;$i++)
{
$line=$line.$words[$i];
$line=$line." ";
}
$line=$line."...";
return htmlentities($line);
}
Sure it's not the most compact way to do it, but at least I hope it's
clear. Also note that I assume the news HAS at least XX words,
altough if this is not true, nothing too bad happens...
I filter the result thru htmlentities() because I have news written in
Italian so I have to deal with $agrave; $egrave; ecc. ecc...
If your news are in english, plain text, you could skip it.
HTH!
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
Fabrizio Ermini Alternate E-mail:
C.so Umberto, 7 faermini@tin.it
loc. Meleto Valdarno Mail on GSM: (keep it short!)
52020 Cavriglia (AR) faermini@sms.tin.it