PDA

View Full Version : Dynamic RSS Feed using PHP


gc40
10-16-2007, 09:28 PM
I am building an RSS feed for my site that is going to use the database entries to populate the latest 15 entries on the RSS feed.
Its been completed and works fine, except two problems.

#1. The description field which is a text field with unlimited characters outputs in pull. I would like to limit this to 200 CHAR. Is there a command I can use to limit the field description from outputting the full article to only allow an output of 200 words?

#2. Whenever there is an apostrophe s (') it displays it in the RSS feed as a triangle like it does not recognize the character. Does anyone have an idea of how I can get the feed to pull database entries with the ' and not have them displayed as triangles with question marks?

Here is the code below:

<? header('Content-type: text/xml'); ?>
<rss version="2.0">
<channel>
<title>MYBLOG</title>
<description>Blog Description</description>
<link>http://www.mydomain.com/</link>
<copyright>http://www.mydomain.com</copyright>
<?
include "./class/config.php";
$result = mysql_query("SELECT * FROM `blog` ORDER BY `BLOGID` DESC LIMIT 15");
while($r=mysql_fetch_array($result))
{
$TITLE=$r["TITLE"];
$BLOG=$r["BLOG"];
$BLOGID=$r["BLOGID"];

echo "<item>";
?>
<title><?=htmlentities(strip_tags($TITLE)); ?></title>
<description><?=htmlentities(strip_tags($BLOG,'ENT_QUOTES'));?></description>
<link>http://www.domain.com/blog.php?id=/<?=$BLOGID?></link>
<?
echo "</item>";
}
?>
</channel>
</rss>

GJay
10-16-2007, 10:21 PM
substr() will return a sub-string for you, take a look at http://php.net/substr for usage- you'll need to check the string is longer than 200 characters first (strlen()) or substr() will return FALSE.

You're passing the string 'ENT_QUOTES' to htmlentities when you should be passing the (numeric) constant, ENT_QUOTES. I'm actually surprised your code works with that error- is it a typo in what you've pasted here?

The ? inside a (normally) diamond means that the character isn't part of the 'charset' that is being used to display the content- htmlentities uses ISO-8859-1 by default, so if your content is using something else, you'll need to change them so they're the same.

gc40
10-17-2007, 03:48 PM
substr() will return a sub-string for you, take a look at http://php.net/substr for usage- you'll need to check the string is longer than 200 characters first (strlen()) or substr() will return FALSE.

You're passing the string 'ENT_QUOTES' to htmlentities when you should be passing the (numeric) constant, ENT_QUOTES. I'm actually surprised your code works with that error- is it a typo in what you've pasted here?

The ? inside a (normally) diamond means that the character isn't part of the 'charset' that is being used to display the content- htmlentities uses ISO-8859-1 by default, so if your content is using something else, you'll need to change them so they're the same.

I am not sure what you mean, but how can I eliminate the database from storing such characters?