casper
casper

Reputation: 861

RSS feed: htmlspecialchars() vs. CDATA

i've written a little class in PHP to generate a simple RSS feed.

The item description should also be able to contain html tags.

I've wondered which one is the better way and what are the advantages/disadvantages of each method:

$item .= "<description><![CDATA[" . $description . "]]></description>\n";

or

$item .= "<description>" . htmlspecialchars($description, ENT_NOQUOTES, "UTF-8") . "</description>\n";

The only disadvantage of the CDATA method i can think of is, that i've to check in advance if the string $description contains

"<![CDATA[" or "]]>".

Thanks in advance for clarification.

Upvotes: 3

Views: 3473

Answers (1)

Dr.Molle
Dr.Molle

Reputation: 117354

There is only 1 choice when you want to use HTML-code there, the CDATA-section.

The reason: The feed must be valid XML, but HTML may not be valid XML. So if you use HTML without CDATA, everything inside the description that is not valid XML will result in a XML-Parser-Error. This may already be forced by such a simple thing like &nbsp; or <br> (what is valid HTML, but invalid XML)

CDATA-sections will not be parsed, so you can use there what you want to.

But however, you better use <content:encoded> for markup.

Upvotes: 1

Related Questions