2012-10-31 42 views
-2

是否有人知道如何只提取具有特定類的div標記,在PHP中?使用PHP提取具有特定類的div標記

<div class="abc"> 
</div> 
<div class="def"> 
</div> 
<div class="xyz"> 
</div> 

我只需要div class =「abc」。 我該如何實現?

+0

可能要求[在PHP中解析HTML的最佳方法](http://stackoverflow.com/questions/3577641/how-to-parse-and-process-html-with-php),但目前NARQ。 – mario

+0

嘗試使用DOM,http://php.net/manual/en/book.dom.php –

回答

1

你想要的是:XPath

<?php 
$html = new DOMDocument(); 
@$html->loadHtmlFile('thepage.html'); 
$xpath = new DOMXPath($html); 
$nodelist = $xpath->query("//*[contains(@class, 'abc')]"); 
foreach ($nodelist as $n){ 
    echo $n->nodeValue."\n"; 
} 
?> 

[更新]

增加了新的XPath查詢。 試試看。

+0

感謝您的回答。但是它不給我我需要的結果。
DOMNodeList Object([length] => 0) – harry

+0

是位於同一臺服務器上的文件嗎?或遠程文件? – Marty

+0

文件是在同一臺服務器上 – harry

相關問題