Sorry if this has been asked before – there’s a question with a similar title but it’s not quite what I’m looking for.
What I am doing is fetching results from a DB and printing them out inside the appropriate tags to create an RSS Feed.
The only problem is the body of the article contains html tags so my rss feed doesn’t load correctly.
So far I have tried:
$rssfeed .= '<description>' . htmlentities($row['text') . '</description>';
and
$rssfeed .= '<description>' . htmlspecialchars($row['text'], ENT_COMPAT, 'UTF-8') . '</description>';
Using either of those just gives me a completely blank feed.
I actually just tried this aswell and got the same result:
$rssfeed .= '<description>' . $row['text']. '</description>';
I’m more confused now as surely that should have worked? The only thing I can think of is that the row in the database is too long for the RSS Feed as it’s a full article worth of text and HTML in there.
I wasn’t sure if it would automatically be truncated or not.
Any ideas?
UPDATE
This is some sample output:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>RSS feed</title>
<link>http://www.12345.co.uk</link>
<description>RSS Feed from 12345.co.uk</description>
<language>en-uk</language>
<copyright>Copyright (C) 2011 12345.co.uk</copyright>
<item>
<title>Wear safe but stylish sunglasses on the slopes</title>
<description><![CDATA["<p>Holidaymakers hitting the slopes this year must remember to pack a pair of sunglasses alongside their skis, one expert has highlighted.</p>
<p>Dharmesh Patel, chairman of the Eyecare Trust, noted that UV protective eyewear is a must for skiers.</p>
<p>Posted by xxxxx</p>v"]]>
</description>
<link></link>
<pubDate>Mon, 03 Oct 2011 16:57:09 +0000</pubDate>
</item>
Since RSS is an XML-Language, you’ll want to use an CDATA section:
In PHP, you can easily archive this (with the
XMLWriter) by using thestartCData()andendCData()-methods.If the XML-Reader displays your HTML correctly depends on the parser it’s using. Using escape-sequences is however valid XML.