2017-05-27 248 views
0

我想使用PHP將以下HTML中的記錄讀入變量。做這個的最好方式是什麼?示例HTML代表兩條記錄。使用PHP解析HTML DOM

結果備案1:

 

    rank = 1 
    tag = LLG8V2QQ 
    name = Pat 
    level = 11 
    league = 1 
    trophies = 4154 
    donations = 578 
    role = Elder 

<div class="clan__rowContainer"> 
    <div class="clan__row"> 
         #1 
       </div> 
    <div class="clan__row"> 
     <a class="ui__blueLink" href='/profile/LLG8V2QQ'>Pat</a> 
    </div> 
    <div class="clan__row"> 
     <span class="clan__playerLevel">11</span> 
    </div> 
    <div class="clan__row"> 
     <div class="clan__leagueContainer"> 
         <div class="league__1"></div> 
        </div> 
    </div> 
    <div class="clan__row"> 
     <div class="clan__cup">4154</div> 
    </div> 
    <div class="clan__row">578</div> 
    <div class="clan__row"> 
     Elder 
       </div> 
    </div> 


    <div class="clan__rowContainer"> 
    <div class="clan__row"> 
         #2 
       </div> 
    <div class="clan__row"> 
     <a class="ui__blueLink" href='/profile/299GGR2J'>Erikson</a> 
    </div> 
    <div class="clan__row"> 
     <span class="clan__playerLevel">11</span> 
    </div> 
    <div class="clan__row"> 
     <div class="clan__leagueContainer"> 
         <div class="league__1"></div> 
        </div> 
    </div> 
    <div class="clan__row"> 
     <div class="clan__cup">4081</div> 
    </div> 
    <div class="clan__row">248</div> 
    <div class="clan__row"> 
     Member 
       </div> 
    </div> 
+0

你可以考慮[捲曲](HTTP ://php.net/manual/en/book.curl.php)。 – Pyromonk

+0

你嘗試過什麼,根據你的PHP代碼? – cfnerd

回答

0

您可以嘗試使用PHP的DOMDocument類。這裏是文檔link。 您可以examle開始:

foreach ($doc->childNodes as $item) { 
    //... some code 
} 
0

到目前爲止我的代碼:

<?php 
$doc = new DOMDocument(); 
$doc->loadHTMLFile("filename.html"); 
//or $doc->loadHTML("<html><body>Test<br></body></html>"); 

然後在文檔節點和他們的子女重複

<?php 
     $_document = implode('', file('http://myURL')); 

     $dom = new DomDocument; 
     $dom->loadHTML($_document); 
     $dom->preserveWhiteSpace = false; 
     $divs = $dom->getElementsByTagName('div'); 
     foreach ($divs as $div) { 
      $class = $div->getAttribute('class'); 
      if ($class == 'clan__rowContainer') { 
       NO IDEA WHAT NOW 
      } 
     } 
    ?>