在php中解析扭曲的HTML页面

在php中解析扭曲的HTML页面,php,html,ajax,html-parsing,Php,Html,Ajax,Html Parsing,我在php文件中调用一个AJAX url,并通过CURL获取其内容。 但是我得到的HTML充满了\r\t和\n。div也被扭曲了。 我该如何处理这个问题。下面是完整HTML文本的一小部分 <html> <head> <title></title> </head> <body> id= "\&quot;moreCount\&quot;">491-500</span>\r\n\r\n\r\n\t

我在php文件中调用一个AJAX url,并通过CURL获取其内容。 但是我得到的HTML充满了\r\t和\n。div也被扭曲了。 我该如何处理这个问题。下面是完整HTML文本的一小部分

<html>
<head>
<title></title>
</head>
<body>
id=
"\&quot;moreCount\&quot;">491-500</span>\r\n\r\n\r\n\t
<div id="\&quot;propSearchMainWrap\&quot;">\r\n\t\t
<div class="\&quot;propSearchMainContent\&quot;">\r\n\t\t\t
<div>\r\n\t\t\t\t\r\n\t\t\t\t\t\r\n\t\t\t\t\t\r\n\t\t\t\t\t\t
<div class="\&quot;searchDtlLeftN\&quot;" style="\&quot;width:">
\r\n\t\t\t\t\t\r\n\t\t\t\t\r\n\t\t\t\t\r\n\t\t\t\t\t<input type=
"\'checkbox\'" name=
"\'checkbox10374276\'\r\n\t\t\t\t\t\tid=\'checkbox10374276\'\r\n\t\t\t\t\t\tonmouseout=\&quot;hideToolTipCheckboxCount(\'tool_tip10374276\');\&quot;\r\n\t\t\t\t\t\tonmouseover=\&quot;showToolTipCheckboxCount(\'tool_tip10374276\',\'Property\');\&quot;">\r\n\t\t\t\t\t
<div id="\'tool_tip10374276\'\r\n\t\t\t\t\t\tstyle=\'display:"
width:="" position:="" z-index:="" padding:=""></div>
\r\n\t\t\t\t\r\n\t\t\t\t <b><a target="\&quot;_blank\&quot;" href=
"/&quot;/propertyDetails/5-BHK-3000-Sq-ft-Residential-House-FOR-Sale-Cookes-Town-in-Bangalore&amp;id=Q8oDBbaV2WFzpSvf+uAgZw==/&quot;">
5 BHK Residential House for Sale in Cookes
Town</a>\r\n\t\t\t\t</b>\r\n\t\t\t</div>
\r\n\t\t\t
<!-- added by narendra -->\r\n\t\t\t\r\n\t\t\t\t\r\n\t\t\t\t\r\n\t\t\t\t\t
<div style="\&quot;width:"></div>
\r\n\t\t\t\t\r\n\t\t\t\r\n\t\t\t
<!-- added by narendra -->\r\n\t\t</div>
\r\n\t\t
<div>\r\n\t\t\t<!--left panel start-->\r\n\t\t\t
<div class="\&quot;searchDetailPanelLft\&quot;">\r\n\r\n\t\t\t\t
<div class="\&quot;searchDetailSubBox1\&quot;">\r\n\t\t\t\t\t
<!--content start-->\r\n\t\t\t\t\t
<div class="\&quot;search_packageImg\&quot;">
\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<img alt="\&quot;Premium\&quot;"
title=
"\&quot;Premium\&quot;\r\n\t\t\t\t\t\t\t\tsrc=\&quot;/images/premium-img.gif\&quot;">\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t\t\r\n\t\t\t\t\t</div>

身份证=
“\“moreCount\”>491-500\r\n\r\n\r\n\t
\r\n\t\t
\r\n\t\t\t
\r\n\t\t\t\r\n\t\t\t\t\t\r\n\t\t\t\t\t\r\n\t\t\t\t\t\t

可以使用preg\u replace删除\r\t和\n

也就是说,您仍然会有一些奇怪的赋值,例如宽度:=。。。这是HTML Pascal吗

    $html = // awful stuff

    // Remove quoted \r, \t and \n
    $html = preg_replace("#\\[rnt]#ms", '', $html);

    // Remove double quotation marks, apparently spurious
    $html = preg_replace('#["]#ms', '', $html);

    // Remove extra escapes
    $html = stripslashes($html);

    // Convert (apparently) original marks back to normal
    $html = HTML_Entity_Decode($html);

可以使用preg\u replace删除\r\t和\n

也就是说,您仍然会有一些奇怪的赋值,例如宽度:=。。。这是HTML Pascal吗

    $html = // awful stuff

    // Remove quoted \r, \t and \n
    $html = preg_replace("#\\[rnt]#ms", '', $html);

    // Remove double quotation marks, apparently spurious
    $html = preg_replace('#["]#ms', '', $html);

    // Remove extra escapes
    $html = stripslashes($html);

    // Convert (apparently) original marks back to normal
    $html = HTML_Entity_Decode($html);

您应该尝试用假定的字符替换所有\x出现的字符。那么DOMDocument的loadHTML就很好了

$html = strtr($html, array('\\\\' => '\\', '\\r' => "\r", '\\n' => "\n", '\\t' => "\t", '\\' => ''));
$doc = new DOMDocument();
$doc->loadHTML($html);
$html = $doc->saveHTML();

您应该尝试用假定的字符替换所有\x出现的字符。那么DOMDocument的loadHTML就很好了

$html = strtr($html, array('\\\\' => '\\', '\\r' => "\r", '\\n' => "\n", '\\t' => "\t", '\\' => ''));
$doc = new DOMDocument();
$doc->loadHTML($html);
$html = $doc->saveHTML();

使用stripcslashes()方法可以轻松解决此问题。
工作起来很有魅力。

使用stripcslashes()方法轻松解决了这个问题。
工作很有魅力。

我认为要从混乱中获得一些有用的内容,还有很多事情要做。更好的解决方案是使用PHP脚本获得干净的HTML.WTF。。。不确定您是如何管理它的,或者不带标记的
id
属性在
body
之后是什么,@Utkanos会被错误地粘贴。主要的问题是\r\n和\t文本出现在整个HTML中。似乎
\r\n\t
被放在单引号中,这就是为什么它们以字符的形式出现在HTML中。更改从ajax调用的文件,并将
\r\n\t
放在双引号中,以允许解析它们,而不是单引号!我认为要从混乱中获得一些有用的内容还有很多事情要做。更好的解决方案是使用PHP脚本获得干净的HTML.WTF。。。不确定您是如何管理它的,或者不带标记的
id
属性在
body
之后是什么,@Utkanos会被错误地粘贴。主要的问题是\r\n和\t文本出现在整个HTML中。似乎
\r\n\t
被放在单引号中,这就是为什么它们以字符的形式出现在HTML中。更改从ajax调用的文件,并将
\r\n\t
放在双引号中,以允许解析它们,而不是单引号!使用stripcslashes()方法可以轻松解决此问题。工作起来很有魅力。使用stripcslashes()方法可以轻松解决此问题。工作得很有魅力。