Php 在XML文件中重新转义字符

Php 在XML文件中重新转义字符,php,html,xml,rss,atom-feed,Php,Html,Xml,Rss,Atom Feed,考虑以下XML结构(在本例中,它是一个RSS提要) 我想包含的最后一行显然使.atom文件无效。因此,我使用以下PHP函数将最后一行编码为XML兼容: echo htmlentities("Now, heres litteral \"<\" and \">\" characters.... <><><<<>>",ENT_XML1); 但是现在,我所有的feed阅读器(用于chrome的Slick RSS和用于android的Fe

考虑以下XML结构(在本例中,它是一个RSS提要)

我想包含的最后一行显然使.atom文件无效。因此,我使用以下PHP函数将最后一行编码为XML兼容:

echo htmlentities("Now, heres litteral \"<\" and \">\" characters.... <><><<<>>",ENT_XML1);
但是现在,我所有的feed阅读器(用于chrome的Slick RSS和用于android的FeedR)都将上述内容解释为文本HTML

那我怎么才能重新摆脱这些呢


干杯:)

因为当解析XML文档时,该字段的内容仍然包含文本
[可能还有其他]元字符

// the literal string you want to encode.
$string1 = "Now, heres litteral \"<\" and \">\" characters.... <><><<<>>";

// oops but I want to make sure I don't accidentally pass in HTML to RSS readers that might
// accidentally try to render it.
$string2 = htmlentities($string1);

// oh also I am writing XML directly instead of using a proper library to generate the document.
// I know that this is a really bad idea, but I'm sure I have my reasons.
// anywho, I should escape this text to be kludged directly into an XML doc.
$string3 = htmlentities($string2, ENT_XML1);

var_dump($string1, $string2, $string3);
//要编码的文本字符串。

$string1=“现在,这里是literal \“\”字符。…您定义了
摘要中的片段是HTML片段

<summary type="html">
Description of post. (Preview thing)
</summary>

帖子描述(预览内容)
Atom支持
type
属性来定义内容的处理方式。它甚至可以像视频一样对二进制内容进行编码


类型
html
读取节点的文本内容并将其呈现为html片段。
text
读取文本内容并将其输出为纯文本。
xhtml
呈现子节点。

感谢您的帮助!但是,为什么制作自己的RSS提要而没有任何库是一个坏主意?这是一种更大的方式(博客)我是从零开始编码的。我为什么要在那里塞进一个库?@keanu_reeves,因为这不是你要花时间重新解决的唯一问题,下一个问题可能会与此冲突一次,以此类推,直到你的代码库变成一个醒着的噩梦,要求用min从零开始重写所有这些问题d、 现有的库通常是已经完成的多次迭代的结果,并且有许多贡献者/维护者、良好的文档和其他开发人员在第一次处理您的项目之前就已经知道如何使用它的额外好处。
Now, heres litteral "&lt;" and "&gt;" characters.... &lt;&gt;&lt;&gt;&lt;&lt;&lt;&gt;&gt;
// the literal string you want to encode.
$string1 = "Now, heres litteral \"<\" and \">\" characters.... <><><<<>>";

// oops but I want to make sure I don't accidentally pass in HTML to RSS readers that might
// accidentally try to render it.
$string2 = htmlentities($string1);

// oh also I am writing XML directly instead of using a proper library to generate the document.
// I know that this is a really bad idea, but I'm sure I have my reasons.
// anywho, I should escape this text to be kludged directly into an XML doc.
$string3 = htmlentities($string2, ENT_XML1);

var_dump($string1, $string2, $string3);
string(56) "Now, heres litteral "<" and ">" characters.... <><><<<>>"
string(109) "Now, heres litteral &quot;&lt;&quot; and &quot;&gt;&quot; characters.... &lt;&gt;&lt;&gt;&lt;&lt;&lt;&gt;&gt;"
string(169) "Now, heres litteral &amp;quot;&amp;lt;&amp;quot; and &amp;quot;&amp;gt;&amp;quot; characters.... &amp;lt;&amp;gt;&amp;lt;&amp;gt;&amp;lt;&amp;lt;&amp;lt;&amp;gt;&amp;gt;"
<summary type="html">
Description of post. (Preview thing)
</summary>