2014-05-14 43 views
1

有人可以幫我一把。用Div PHP PHP Dom

我試圖從一些頁面獲取信息,看起來像這樣的HTML。

<div class="block"> 
    <h2>Season 1</h2> 
    <div class="episode"><a href="somelink.com">Episode 1</a></div> 
    <div class="episode"><a href="somelink.com">Episode 2</a></div> 
    <h2>Season 2</h2> 
    <div class="episode"><a href="somelink.com">Episode 1</a></div> 
</div> 

但是,我堅持上是每個賽季,我想包起來的div在div裏面的季節發作例如

<div class="block"> 
    <div class="season"> 
     <h2>Season 1</h2> 
     <div class="episode"><a href="somelink.com">Episode 1</a></div> 
     <div class="episode"><a href="somelink.com">Episode 2</a></div> 
    </div> 
    <div class="season"> 
     <h2>Season 2</h2> 
     <div class="episode"><a href="somelink.com">Episode 1</a></div> 
    </div> 
</div> 

而且PHP代碼我與

工作
$page = "someurl.com"; 

$page = $this->curl->get($page); 
$dom = new DOMDocument(); 
@$dom->loadHTML($page); 

$divs = $dom->getElementsByTagName('div'); 
for($i=0;$i<$divs->length;$i++){ 
    if ($divs->item($i)->getAttribute("class")=="block") { 
    $h2s = $divs->item($i)->getElementsByTagName('h2'); 
    if (count($h2s) > 0) { 
     foreach ($h2s as $h2) { 
     // Stuck at this point 
     } 
    } 
    } 
} 

我該如何在PHP DOM中做到這一點,有人請給我一個例子謝謝。

+2

Regardlass誰可能解決這個問題爲您,我們都喜歡你去嘗試,並且告訴你在你的問題已經試過什麼。這樣你就可以瞭解你做錯了什麼/不正確。 – bestprogrammerintheworld

+0

更新我的問題 – user3375691

+0

你用什麼來表示/解析DOM結構? –

回答

1

下面的代碼包每個<h2>及其.episode兄弟姐妹.season容器

$page = '<div class="block"> 
     <h2>Season 1</h2> 
     <div class="episode"><a href="s1ep1.com">Episode 1</a></div> 
     <div class="episode"><a href="s1ep2.com">Episode 2</a></div> 
     <h2>Season 2</h2> 
     <div class="episode"><a href="s2ep1.com">Episode 1</a></div> 
     <div class="episode"><a href="s2ep1.com">Episode 2</a></div> 
    </div>'; 

    $dom = new DOMDocument(); 

    $origVal = libxml_use_internal_errors(true); 
    @$dom->loadHTML($page); 
    libxml_clear_errors(); 
    libxml_use_internal_errors($origVal); 

    //create a tmeplate 'season' div 
    $season = $dom->createElement('div'); 
    $season->setAttribute('class', 'season'); 

    //get all '.block' divs using xpath 
    $xpath = new DOMXPath($dom); 
    $divs = $xpath->query("//*[@class='block']"); 

    $clones = array(); 
    $clone = ''; 

    foreach($divs as $currDiv) { 

    //check if the 'block' contains any <h2> elemnts, if not, skip this block 
    if(!count($currDiv->getElementsByTagName('h2'))) { 
     continue; 
    } 

    foreach($currDiv->childNodes as $child) { 

     if(in_array($child->nodeName, array(
              '#text', 
              '#comment' 
            )) 
     ) { 
      //ignore white space (and text content), and comments in 'block' div 
      continue; 
     } 

     if($child->nodeName == 'h2') { 
      if($clone) { 
       //save all clones of 'season' template div in an array for further use 
       $clones[] = $clone; 
      } 

      $clone = $season->cloneNode(true); 
     } 

     //this is the tricky part. If we do not append a clone of original div, then it actually moves the div to $clone. This changes HTML structure and disrupts the current loop 
     //so we append the clones of child to the 'season' div 
     if($child->nodeName == 'h2' || $child->getAttribute('class') == 'episode') { 
      $clone->appendChild($child->cloneNode(true)); 
     } 
    } 
    $clones[] = $clone; 

    //remove all children of current 'block' div 
    while($currDiv->childNodes->length) { 
     $currDiv->removeChild($currDiv->firstChild); 
    } 

    //isnert all 'season' nodes in it 
    foreach($clones as $c) { 
     $currDiv->appendChild($c); 
    } 
    } 

    echo $dom->saveHTML(); 
+0

我非常感謝您花時間寫這篇文章。非常感謝你:) – user3375691