tags:

views:

178

answers:

4

I have written a simple program to generate rss feed from mysql db. but it broke because of the reason that there were several characters in the data which were special like '&' which made RSS feed go bad.

I am sure there would be some easy to use library which would take care of these things and I dont have to find replace such data( which sounds such a bad idea).

Please advice php library to generate to rss feed, which are generally available on hosting service providers liks hostmonster

+1  A: 

http://php.net/manual/en/function.htmlentities.php

This applies to XML (RSS feeds are XML) as well.

Aequitarum Custos
+1  A: 

Why not just sanitise the posts from the database before putting them in the feed? You can easily use php's inbuilt html_entities() function.

eg:

$post = 'This came from the database and contains nasty characters like &!';
$sanitised = htmlentities($post);

Now $sanitised equals "This%20came%20from%20the%20database%20and%20contains%20nasty%20characters%20like%20%26!" which should display fine in any rss reader.

PhantomCode
This almost worked. Now I see some characters like this ;€™s which is breaking my feed.Any suggestions
JewelThief
I'd suggest going with your first instinct, and using a library.
Tim Lytle
+2  A: 

While encoding the data isn't really that hard, using a library to generate a good compatible RSS or Atom feed is probably a good solution (and, after all is what you've asked for).

Zend_Feed provides feed creation in addition to feed reading.

Taken from the Zend_Feed documentation:

// importing a rss feed from an array
$feed = Zend_Feed::importArray($array, 'rss'); 

// dump the feed to standard output
print $feed->saveXML();

// send http headers and dump the feed
$feed->send();

As to availability on hosts, some may have the Zend Framework in their include directory. But if not, it's easy to just copy the Zend Framework libraries you need (in your case, the Zend_Feed files) someplace in your hosting directory.

Tim Lytle
A: 

I'm using SimpleXMLElement to generate my RSS. It's crazy simple to use:

$elementA->nestedElementB->elementC['AttributeD'] = $str_value

And all the escaping and encoding is taken care of.

Ivan Krechetov