2016-05-12 65 views
0

我玩一個名爲Tribalwars的在線遊戲,現在正在嘗試編寫報告解析器。一個典型的報告是這樣的:DOMDocument missing HTML tags

https://enp2.tribalwars.net/public_report/395cf3cc373a3b8873c20fa018f1aa07

我有改編自php.net,現在看起來兩個功能如下:

function has_child($p) 
{ 
    if ($p->hasChildNodes()) 
    { 
     foreach ($p->childNodes as $c) 
     { 
      if ($c->nodeType == XML_ELEMENT_NODE) 
      { 
       return true; 
      } 
     } 
    } 
    return false; 
} 

function show_node($x) 
{ 
    foreach ($x->childNodes as $p) 
    { 
     if ($this->has_child($p)) 
     { 
      $this->show_node($p); 
     } 
     elseif ($p->nodeType == XML_ELEMENT_NODE) 
     { 
      if (trim($p->nodeValue) !== '') 
      { 
       $temp = explode("\n", $p->nodeValue); 
       if (count($temp) == 1) 
       { 
        $this->reportdata[] = trim($temp[0]); 
       } 
       else 
       { 
        foreach ($temp as $k => $v) 
        { 
         if (trim($v) !== '') 
         { 
          $this->reportdata[] = trim($v); 
         } 
        } 
       } 
      } 
     } 
    } 
} 

它返回結果的格式如下:

Array 
(
    [0] => MASHAD (27000) attacks 40-014-Devil... 
    [1] => May 11, 2016 19:27:12 
    [2] => MASHAD has won 
    [3] => Attacker's luck 
    ... 
    [76] => Espionage 
    [77] => Resources scouted: 
    [78] => Building 
    ... 
    [112] => Haul: 
    [113] => . 
    [114] => . 
    [115] => . 
    [116] => . 
    [117] => . 
    ... 
    [120] => https://enp2.tribalwars.net/public_report/395... 
) 

大多數情況下,這種方法可行,但有些數據在解析中會丟失。如果您查看鏈接中的報告,您會看到「資源搜索」和「拖拉」部分。順便說一下,這兩部分都包含<span>。由於某些原因,函數返回的數組中缺少這兩個部分。 (請參閱數組項目77和數組項113 - 118)。第113 - 118行只顯示奇怪格式數的.,第77行沒有任何內容。

在我所說的show_node()功能的功能,我做加工的一點點拋棄所不需要不必要的DOM代碼:

$temp = explode('<h1>Publicized report</h1>', $report[0]['reportdata']); 
$rep = $temp[1]; 
$temp = explode('For quick copy and paste', $rep); 
$rep = '<report>' . $temp[0] . '</report>'; 
$x = new DOMDocument(); 
$x->loadHTML($rep); 
$this->show_node($x->getElementsByTagName('report')->item(0)); 

如果我做的$rep輸出調用show_node()前功能,我需要的信息HaulResources scouted存在。

可能是什麼問題?

回答

0

它看起來好像DOMDocument對文檔的深度有限制或者其他內容。無論是那個還是上面的遞歸代碼都是錯誤的。因此,我確定了一段未被解析的代碼,看到它的格式良好,然後繼續刪除我不需要的子項,最後在數組中獲取值。無論如何,這個問題現在已經解決了。