这是我的示例脚本:
$html = <<
Capture this text 1
May 2010
Capture this text 2
June 2010
HTML;
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[@class="main"]');
foreach ($tags as $tag) {
print_r($tag->nodeValue."\n");
}
这将是:
Capture this text 1 May 2010
Capture this text 2 June 2010
但我需要它输出:
Capture this text 2
June 2010
或者至少可以在我的foreach循环中做这样的事情:
$text = $tag->query('//div[@class="text"]')->nodeValue;
$date = $tag->query('//div[@class="date"]')->nodeValue;