1

I want to read records from the following HTML into variables, using PHP. What is the best way to do this? The example HTML represents two records.

Result for record 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>
1
  • You can consider cURL. Commented May 27, 2017 at 13:59

2 Answers 2

1

My code so far:

    <?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
            }
        }
    ?>
Sign up to request clarification or add additional context in comments.

Comments

0

You can try using PHP's DOMDocument class. Here's the documentation link . You can for examle start with:

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

then iterate over doc nodes and their children:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.