Simplexml not loading <a> tag classes?

I have a little php that grabs html from a page and loads it into a simplexml object. However, not getting the element classes inside

php

//load the html page with curl
$html = curl_exec($ch);
curl_close($ch);

$doc = new DOMDocument();
$doc->loadHTML($html);
$sxml = simplexml_import_dom($doc);

Html page. Which, if I do var_dump from $ html, shows that it has been cleared and exists in $ html

    <li class="large">
        <a style="" id="ref_3" class="off" href="#" onmouseover="highlightme('07');return false;" onclick="req('379');return false;" title="">07</a>
    </li>

The var_dump parameter (below) from $ doc and $ sxml indicates that the class 'off' is now missing. Unfortunately, I need to process a page based on this class.

            [8]=>
             object(SimpleXMLElement)#50 (2) {
              ["@attributes"]=>
              array(1) {
                ["class"]=>
                string(16) "large"
              }
              ["a"]=>
              string(2) "08"
            }
+4
source share
1 answer

Using simplexml_load_fileand xpath, see inline comments.

What you need, really, as soon as you find the right item, it’s

$row->a->attributes()->class=="off"

And the full code below:

// let take all the divs that have the class "stff_grid"
$divs = $xml->xpath("//*[@class='stff_grid']");

// for each of these elements, let print out the value inside the first p tag
foreach($divs as $div){
    print $div->p->a . PHP_EOL;

    // now for each li tag let print out the contents inside the a tag
    foreach ($div->ul->li as $row){

        // same as before
        print "  - " . $row->a;
        if ($row->a->attributes()->class=="off") print " *off*";
        print PHP_EOL;

        // or shorter
        // print "  - " . $row->a . (($row->a->attributes()->class=="off")?" *off*":"") . PHP_EOL;

    }
}
/* this outputs the following
Person 1
  - 1 hr *off*
  - 2 hr
  - 3 hr *off*
  - 4 hr
  - 5 hr
  - 6 hr *off*
  - 7 hr *off*
  - 8 hr
Person 2
  - 1 hr
  - 2 hr
  - 3 hr
  - 4 hr
  - 5 hr
  - 6 hr
  - 7 hr *off*
  - 8 hr *off*
Person 3
  - 1 hr
  - 2 hr
  - 3 hr
  - 4 hr *off*
  - 5 hr
  - 6 hr
  - 7 hr *off*
  - 8 hr
*/
+1

Source: https://habr.com/ru/post/1612473/


All Articles