【问题标题】:Parsing a distorted HTML page in php在 php 中解析扭曲的 HTML 页面
【发布时间】:2012-07-06 11:33:17
【问题描述】:

我在 php 文件中调用 AJAX url 并通过 CURL 获取其内容。 但是我得到的 HTML 充满了 \r,\t 和 \n.Divs 也被扭曲了。 我该如何处理。这是完整 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>

【问题讨论】:

  • 我认为要从混乱中获得一些有用的内容需要做很多事情。更好的解决方案是使用 PHP 脚本来获得干净的 HTML。
  • WTF...不知道你是怎么做到的,或者在body之后那个无标签id属性是什么...
  • @Utkanos 那会被错误地粘贴。主要关注的 \r ,\n 和 \t 文本出现在整个 HTML 中。
  • 似乎 \r\n\t 被放在单引号中,这就是为什么它们作为字符出现在 html 中。更改从 ajax 调用的文件并将 \r\n\t 放在双引号中以允许解析它们,而不是单引号!

标签: php html ajax html-parsing


【解决方案1】:

\r、\t 和 \n 可以用 preg_replace 删除。

也就是说,你仍然会有一些奇怪的赋值,比如 width:= ... 这是 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);

【讨论】:

  • 使用 stripcslashes() 方法轻松解决。像魅力一样工作。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    使用 stripcslashes() 方法轻松解决。 像魅力一样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-30
      • 2014-01-08
      • 1970-01-01
      • 2021-07-08
      • 2020-10-30
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多