2012-03-30 2 views
1

내가 HTML 구문 분석에 대한 정규식을 사용하지만 다음 표 구문 분석하는 당신의 도움이 필요 파싱 된 HTML DOM : 나는 예를 들어 자신의 (배열 또는 VAR) 볼륨 모든 도메인을 얻으려면PHP 정규식 또는

  <table class="resultstable" width="100%" align="center"> 
       <tr> 
        <th width="10">#</th> 
        <th width="10"></th> 
        <th width="100">External Volume</th> 
       </tr>     
       <tr class='odd'> 
         <td align="center">1</td> 
         <td align="left"> 
          <a href="#" title="http://xyz.com">http://xyz.com</a> 
          &nbsp; 
         </td> 
         <td align="right">210,779,783<br />(939,265&nbsp;/&nbsp;499,584)</td> 
        </tr> 

        <tr class='even'> 
         <td align="center">2</td> 
         <td align="left"> 
          <a href="#" title="http://abc.com">http://abc.com</a> 
          &nbsp; 
         </td> 
         <td align="right">57,450,834<br />(288,915&nbsp;/&nbsp;62,935)</td> 
        </tr> 
      </table> 

http://xyz.com - 210,779,783 

이 경우 regex 또는 HTML dom을 사용해야합니까? 큰 테이블을 파싱하는 법을 모르겠다. 도와 줘서 고마워. 고마워.

+2

거의 항상 HTML DOM을 사용해야합니다. 이 경우도 마찬가지입니다. –

+2

[이 질문] (http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)을 참조하십시오. ** 정규식을 사용하여 ** HTML을 구문 분석해서는 안됩니다. –

+0

@Truth 당신은 HTML DOM으로 나를 도울 수 있습니까? HTML DOM을 사용하여 방금 큰 테이블이 아닌 간단한 구문 분석을 할 수 있습니다. 감사. – seoppc

답변

1

여기에 질문에서 HTML을 구문 분석하는 XPath 예제가 있습니다.

<?php 
$dom = new DOMDocument(); 
$dom->loadHTMLFile("./input.html"); 
$xpath = new DOMXPath($dom); 

$trs = $xpath->query("//table[@class='resultstable'][1]/tr"); 
foreach ($trs as $tr) { 
    $tdList = $xpath->query("td[2]/a", $tr); 
    if ($tdList->length == 0) continue; 
    $name = $tdList->item(0)->nodeValue; 
    $tdList = $xpath->query("td[3]", $tr); 
    $vol = $tdList->item(0)->childNodes->item(0)->nodeValue; 
    echo "name: {$name}, vol: {$vol}\n"; 
} 
?>