【发布时间】:2012-12-12 17:37:40
【问题描述】:
我正在做一些 HTML DOM 操作:
function parse_html($html) {
$dom->loadHTML($html);
libxml_clear_errors();
// Parse DOM
return $dom->saveHTML();
}
问题是我的 HTML 包含一些 PHP 代码,其中一些被转换为 HTML 实体。例如,如果$html 包含以下内容:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<?php // lang=es
$pwd = $parameter['pwd'];
$url = $parameter['url'];
?>
<p>
You are now registered. Go to ->
<a href="<?php echo $url ?>">control panel</a>
to change the settings.
</p>
变成了这样:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head><meta http-equiv="content-type" content="text/html; charset=UTF-8"></head>
<body>
<?php // lang=es
$pwd = $parameter['pwd'];
$url = $parameter['url'];
?><p> You are now registered. Go to -> <a href="<?php%20echo%20%24url%20?>">control panel</a> to change the settings.
</p>
</body>
</html>
<?php echo $url ?> 被转换为实体,但我不能使用像 *html_entity_decode* 这样的函数,因为它还会解码一些必须保持实体的实体。
如何解析包含 PHP 代码的 DOM?
【问题讨论】:
-
您是否可以选择先通过 PHP 预处理器运行 HTML?这将为您提供纯 HTML,然后您可以对其进行解析。
-
好吧,如果你去彩虹乐园,你可以用你最喜欢的颜色涂上所有的东西,然后重新排列它们而不受物理限制。在编程中,我们通常严格遵守所谓的标准,并且在 DomDocument 中 PHP 是未定义的指示。否则它会发生你所面临的。
-
@user1161318 不,这是项目本身的预解析器
-
@Ivon:如果是这样,请切换到有效的 X(HT)ML,PHP 为processing instructions。
-
也许用解析器可以处理的占位符替换所有 实例,然后再将代码弹出?