2012-10-31 5 views
-2

PHP에서 특정 클래스의 div 태그 만 추출하는 방법을 알고 있습니까?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 개체 ([길이] => 0) – harry

+0

은 같은 서버에있는 파일입니까? 또는 원격 파일? – Marty

+0

동일한 서버에있는 파일 – harry

관련 문제