PHP DOM中的无效XML / HTML问题 [英] Problem with invalid XML/HTML in PHP DOM

查看:84
本文介绍了PHP DOM中的无效XML / HTML问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有以下HTML:

Suppose you have the following HTML:

<style><input><div name="myDiv"></div></style>

您要将其加载到PHP DOMDocument对象中,应该怎么做?如果您使用 $ doc-> loadHTML(),则会出现< div> 内的问题< style> 标签。如果您使用 $ doc-> loadXML(),则会出现< input> 标签的问题

You want to load it into a PHP DOMDocument object, how should you do it? If you use $doc->loadHTML() it will have the problem that the <div> is inside the <style> tag. If you use $doc->loadXML() it will have the problem that the <input> tag doesn't close.

注意:我无法编辑HTML,只能使用PHP来解析它,因为我正在刮擦。

Note: I can't edit the HTML, only the PHP used to parse it, because I'm scraping here.

推荐答案

尝试这样:

$doc = new DOMDocument;
$doc->recover = true;
$doc->loadXml($response);

$ doc-> recover = true 告诉DOMDocument尝试解析非格式良好的文档。有关详细信息,请参阅文档

The $doc->recover = true tells DOMDocument to try and parse non-well formed documents. See the documentation for more information.

这篇关于PHP DOM中的无效XML / HTML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆