简体   繁体   中英

RSS Feed & Special Characters

I have been working for hours on trying to get a rss feed. The problem is with special characters. I have data listed as a list on the website, but I want a rss feed also. The title has "&" which are written as $amp;. If I use htmlspecialchars it converts it to $amp;amp; if I don't the validator gives an error because of the "&". I can't write "&" or the html will not display it correctly. I try to leave it alone and put it in a Cdata tag but that did not work either.

Here is the feed: http://montanafarmersunion.com/rss.php

Here is the html of the same list: http://montanafarmersunion.com/?hd=news&id=news

The RSS Advisory Board page sums up the problem fairly nicely:

The specification has lacked clarity regarding whether HTML is permitted in elements other than an item's description, leading to wide variance in how aggregators treat character data in other elements. This makes it especially difficult for a publisher to determine how to encode the characters "&" and "<", which must be encoded in XML.

Which is to say, there isn't a 'right' way. In theory, you should be able to get away with not using the CDATA tags, and then HTML encoding your title. For example, this works for me in Firefox & IE8:

$title = 'August 1st: MFU President &amp; friends on farm bill';
echo '<title>'.trim($title).'</title>';

However the W3 RSS validator (is this what you are using?) recommends against it because of the &amp; , based on the page linked above. They suggest using the hexadecimal character reference, but only for & and < . The easiest way to implement this is probably a simple str_replace :

$title = 'August 1st: MFU President & friends on farm bill';
$title = str_replace(array('&', '<'), array('&#x26;', '&#x3C;'), $title);
echo '<title>'.trim($title).'</title>';

(note I've made the starting string a bare & )

I feel compelled to mention this blog post as well, which demonstrates that there isn't really a way to make all readers happy all of the time. But the last method should get most of them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM