简体   繁体   English

如何在RSS源中添加换行符?

[英]How to add line breaks in RSS feeds?

I'm building my own custom RSS feed in PHP. 我正在用PHP构建自己的自定义RSS提要。 I want the tag to contain line breaks to make the text more readable. 我希望标记包含换行符以使文本更具可读性。 However, I can't seem to figure out how to do it correctly. 但是,我似乎无法弄清楚如何正确地做到这一点。 No matter what I try some RSS reader interprets it incorrectly. 无论我尝试什么,一些RSS阅读器都会错误地解释它。 Is there some standard best way to add a line-break in and RSS 2.0 feed? 是否有一些标准的最佳方式来添加换行符和RSS 2.0 Feed?

I have tried "\\n", which works in NetNewsWire on the Mac, but gets ignored by the built-in Safari browser's RSS reader. 我尝试过“\\ n”,它在Mac上的NetNewsWire中运行,但内置的Safari浏览器的RSS阅读器会被忽略。

I have tried <br />, which works in the Safari RSS reader, but results in all the text after the 我已经尝试了<br />,它可以在Safari RSS阅读器中使用,但会产生所有文本
being cut off in NetNewsWire. 被NetNewsWire切断。

By default, readers will try and parse your data unless you tell them not to. 默认情况下,读者会尝试解析您的数据,除非您告诉他们不要。 To have them skip over it and present it as you intend, you have to declare a CDATA section in the RSS . 要让它们跳过它并按照您的意图显示它,您必须在RSS中声明一个CDATA部分

If the raw data already has newlines, then you should also be able to just use the nl2br() function to add in the <br /> like so: 如果原始数据已经有换行符,那么你也应该只需要使用nl2br()函数来添加<br />如下所示:

echo '<description><![CDATA[ ' .nl2br($desc_data). ' ]]></description>';

If you don't declare the CDATA section, the RSS readers will see any HTML tags you might have as part of the actual RSS and expect an actual node or element of the RSS feed. 如果您没有声明CDATA部分,RSS阅读器将看到您可能拥有的任何HTML标记作为实际RSS的一部分,并期望RSS源的实际节点或元素。

You can use CDATA and html line breaks: <br/> 您可以使用CDATA和html换行符:<br/>
Example: 例:

<![CDATA[Hi Rss feed<br/> <![CDATA [Hi Rss feed <br />]
Here is new line ]]> 这是新行]]>

The RSS specification states that yes, you can use HTML in a description, but of course it needs to be properly escaped because it is embedded in XML. RSS规范声明是的,您可以在描述中使用HTML,但当然它需要被正确转义,因为它嵌入在XML中。 So using a <br> is the right idea, but you need to encode it using either of these methods, take your pick: 因此,使用<br>是正确的想法,但您需要使用以下任一方法对其进行编码,请选择:

<description>first line&lt;br&gt;second line</description>

<description><![CDATA[first line<br>second line]]></description>
file_put_contents("rss.txt","<br>") roach idea

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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