解析Script标签中的CDATA部分时PHP DOMDocument LoadHTML的问题

时间:2022-10-20 18:31:49

I have been getting this error when trying to parse HTML with a script tag using PHP's DOMDocument class.

尝试使用PHP的DOMDocument类使用脚本标记解析HTML时,我遇到了此错误。

Warning: DOMDocument::loadHTML(): Unexpected end tag : style in Entity, line: 71 in /var/www/signage-dev/dynaml/includes/page.php on line 95

I have even tried CDATA tags but it has resulted in the same error

我甚至尝试过CDATA标签但是它导致了同样的错误

My script tag is

我的脚本标签是

<script type="text/javascript">
//<![CDATA[
document.write('<style type="text/css"> .printBTN { display: inline; } </style>');
//]]>   
</script>

This snippet is supposed to show a print button when JavaScript is enabled. I don't believe this is the most effective way of doing this (the previous developers created this), but even so, I wish to find a solution to this problem since I may run into this problem again in the future and it may not be as easy to fix as this is.

启用JavaScript时,此代码段应显示打印按钮。我不相信这是最有效的方法(以前的开发人员创建了这个),但即便如此,我希望找到解决这个问题的方法,因为我将来可能再遇到这个问题而且可能不会就像这样容易修复。

1 个解决方案

#1


2  

This question seems to have the solution Why split the tag when writing it with document.write()?

这个问题似乎有解决方案为什么在用document.write()编写标签时拆分标签?

I guess php DOM is more strict than most browsers. The solution I have seen sometimes is the one posted in a comment below the linked answer, escaping it with <\/script>, or in this case <\/style>, but I thought it was only necessary with script tags.

我猜php DOM比大多数浏览器都严格。我有时看到的解决方案是在链接答案下方的评论中发布的解决方案,使用<\ / script>转义,或者在这种情况下<\ / style>,但我认为只有脚本标记才需要。

#1


2  

This question seems to have the solution Why split the tag when writing it with document.write()?

这个问题似乎有解决方案为什么在用document.write()编写标签时拆分标签?

I guess php DOM is more strict than most browsers. The solution I have seen sometimes is the one posted in a comment below the linked answer, escaping it with <\/script>, or in this case <\/style>, but I thought it was only necessary with script tags.

我猜php DOM比大多数浏览器都严格。我有时看到的解决方案是在链接答案下方的评论中发布的解决方案,使用<\ / script>转义,或者在这种情况下<\ / style>,但我认为只有脚本标记才需要。