【发布时间】:2016-01-29 07:12:32
【问题描述】:
我正在尝试在 php 中使用 XPATH 获取内容。
<div class='post-body entry-content' id='post-body-37'>
<div style="text-align: left;">
<div style="text-align: center;">
Hi
</div></div></div>
我正在使用下面的 php 代码来获取输出。
$dom = new DOMDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$xpath->registerPhpFunctions('preg_match');
$regex = 'post-(content|[a-z]+)';
$items = $xpath->query("div[ php:functionString('preg_match', '$regex', @class) > 0]");
dd($items);
它返回如下输出
DOMNodeList {#580
+length: 0
}
【问题讨论】:
-
不应该分隔正则表达式吗?试试
$regex = '/post-(content|[a-z]+)/'; -
对不起,我的错字了。我用 $regex = '/post-(content|[a-z]+)/';在代码中
-
见this demo,你不需要正则表达式。
-
是的。但我正在寻找以 post- 开头并包含 content* 的课程。正如我在上面的代码中提到的,我使用上面的正则表达式,因为有些将具有和一些将具有。我需要解析这两个选项。如果你看第一个,它包含两个类*post-body entry-content,如果你看第二个,它只包含一个类**post-conten。我也需要度过难关。
标签: php regex dom xpath domxpath