【发布时间】:2018-11-20 18:42:14
【问题描述】:
我有这个 HTML 模板:
<center>
<img src="image1">
<br><br>
<img src="image2">
<br><br>
<strong><em>TITLE1 :</em></strong> DESC1<br>
<strong><em>TITLE2 :</em></strong> DESC2<br>
<strong><em>TITLE3 :</em></strong> DESC3<br>
<strong><em>TITLE4 :</em></strong> DESC4<br>
<strong><em>TITLE5 :</em></strong> DESC5<br><br><br>
<img src="image3">
<br><br><br>DESC_GEN
</center>
我想使用 xpath 来得到这个预期的结果:
TITLE 1 = DESC 1
TITLE 2 = DESC 2
TITLE 3 = DESC 3
TITLE 4 = DESC 4
TITLE 5 = DESC 5
general = DESC_GEN
在一个数组中,这样我就可以在我的代码中的其他地方使用这些值。
这是我尝试过的:
$dom = new DOMDocument();
$dom->loadHTML($html_string);
$xpath = new DOMXpath($dom);
$elements = $xpath->query("//em");
foreach($elements as $e) {
echo $e->nodeValue . '<br/>';
}
但不幸的是,这只返回 TITLE 1、TITLE 2、TITLE 3 等。
我想获取它们各自的值(在本例中为 DESC 1、DESC 2 等...)。
我可以采取什么方法来实现这个目标?
【问题讨论】:
标签: php html xpath domdocument