如何阻止 PHP Domdocument::SaveXML 插入“CDATA”?
我使用 PHP 从网页获取所有“script”标签,然后在 后面附加文本。这并不总是有效的 html。因为它并不总是有效的标记,所以我不能只使用appendchild/replacechild来添加该信息,除非我误解了replacechild的工作原理。
不管怎样,当我这样做时,
$script_tags = $doc->getElementsByTagName('script');
$l = $script_tags->length;
for ($i = $l - 1; $i > -1; $i--)
$script_tags_string = $doc->saveXML($script_tags->item($i));
这会放“”围绕脚本标签的内容。我怎样才能禁用这个功能?请不要告诉我事后删除它,如果我找不到解决方案,我就会这么做。
I'm using PHP to get all the "script" tags from web pages, and then appending text after the </script> that is not always valid html. Because it's not always valid markup I can't just use appendchild/replacechild to add that information, unless I'm misunderstanding how replacechild works.
Anyway, when I do
$script_tags = $doc->getElementsByTagName('script');
$l = $script_tags->length;
for ($i = $l - 1; $i > -1; $i--)
$script_tags_string = $doc->saveXML($script_tags->item($i));
This puts "<![CDATA[" and "]]>" around the contents of the script tag. How can I disable this? Please don't tell me to just delete it afterwards, that's what I'm going to do if I can't find a solution for this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我怀疑插入了 CDATA,因为否则它会是无效的 XML。
您是否尝试过使用
saveHTML
而不是saveXML
?I have a suspicion that the CDATA is inserted because it would otherwise be invalid XML.
Have you tried using
saveHTML
instead ofsaveXML
?我发现解决此问题的一种方法是:
在回显文档之前,围绕所有脚本标记进行循环,并使用 str_replace 来替换“<”、“>”对于某些字符串,请确保仅在脚本标记内使用该字符串。
然后,在变量中使用 saveXML() 方法,最后使用 str_replace 将“STRING”替换为“<”或“>”
这是代码:
如您所见,现在您可以随意使用“<” “>”在你的脚本中。
希望这对某人有帮助。
One way I've found to fix this:
Before echoing the document, make a loop around all script tags, and use str_replace for "<", ">" to some string, make sure to only use that string inside script tags.
Then, use the method saveXML() in a variable, and finally use str_replace replacing "STRING" to "<" or ">"
Here is the code:
As you can see, now you are free to use "<" ">" in your scripts.
Hope this helps someone.