Web Hosting Talk







View Full Version : Help - limit Description in RSS feed.


e-zone
07-14-2007, 08:29 AM
Got a RSS feed but it displays the hole article in the description on the feed.

i only want it to show the first 100 letters.

Im not a wiz at PHP so any help appreciated.

Here is the code:

// RSS FEED
function rss() {
$limit = s('rss_limit');
$query = "SELECT * FROM articles WHERE position = 1 ORDER BY date DESC LIMIT 0, $limit";
$result = mysql_query($query);
$filename = "rss.xml";
$header = "<?xml version=\"1.0\" ?>";
$header .= "<rss version=\"2.0\">";
$header .= "<channel>";
$header .= "<title>" .s('website_title'). "</title>";
$header .= "<description>" .s('website_title'). "</description>";
$header .= "<link>" .s('website'). "</link>";
$header .= "<copyright>Copyright " .s('website_title'). "</copyright>";
$footer = "</channel>";
$footer .= "</rss>";
$fh = fopen($filename, "w+");
fwrite($fh, $header);
while ($r = mysql_fetch_assoc($result)){
$date = date(s('rss_date_format'), strtotime($r['date']));
$pattern="'<[\/\!]*?[^<>]*?>'si";
$replace="";
$description = preg_replace($pattern, $replace, stripslashes($r['text']));
$item ="<item>";
$item .= "<title>". $r['title'] ."</title>";
$item .= "<description>". $description ."</description>";
$item .= "<pubDate>". $date ."</pubDate>";
$item .= "<link>". s(website) . find_cat_sef($r['category']). "/" .$r['seftitle']. "/</link>";
$item .= "</item>";
fwrite($fh, $item);
}
fwrite($fh, $footer);
fclose($fh);
echo "<script>self.location='" .s('website'). "rss.xml';</script>";
}

Thanks.

horizon
07-14-2007, 08:42 AM
You have two possibilities here:

1 - You can find the settings from that particular function inside your software:


s('rss_limit'); // 's' is the function name and 'rss_limit' is the case name condition inside of it.


2 - You can change this line:


$query = "SELECT * FROM articles WHERE position = 1 ORDER BY date DESC LIMIT 0, $limit";


to:


$query = "SELECT * FROM articles WHERE position = 1 ORDER BY date DESC LIMIT 0, 100";



Although, if you consider to take the 2nd option, it could also affect other places on where the $limit variable is being requested from an SQL query since you're about to modify another function which might also be dependable from other files from your software.

foobic
07-14-2007, 09:41 AM
I believe e-zone simply wants to show the first 100 characters of the description string:
$description = preg_replace($pattern, $replace, stripslashes($r['text']));
$description = substr($description, 0, 100);
$item ="<item>";

e-zone
07-14-2007, 09:45 AM
foobic, exactly what i want, i will try it out thanks.

horizon
That rss limit controls how many articles there should be.

horizon
07-14-2007, 10:03 AM
That rss limit controls how many articles there should be.


I thought this was the question actually ... would seem that yours is the correct one. :)

e-zone
07-14-2007, 12:48 PM
Got it working, thanks

i even got the feed to work probably so it can validate..