简体   繁体   中英

Convert html tags to php array

I have a system status page from a partner that I am trying to convert so that I can automate its input into my own service page. The status of the service is provided by a <span> class. I know this is far from ideal but this is the only way that they provide updates.

Below is a copy of the HTML:

<ul class="item-3">
    <li class="status service1">
        <span class="icon"></span>
        <h6>
        SERVICE 1
        </h6>
        <p>
        Updated:
        9:52 AM CST Apr, 24
        </p>
        <span class="arrow up"></span>
    </li>
    <li class="status service2">
        <span class="icon"></span>
        <h6>
        SERVICE 2
        </h6>
        <p>
        Updated:
        9:52 AM CST Apr, 24
        </p>
        <span class="arrow up"></span>
    </li>
    <li class="status service3">
        <span class="icon"></span>
        <h6>
        SERVICE 3
        </h6>
        <p>
        Updated:
        9:52 AM CST Apr, 24
        </p>
        <span class="arrow up"></span>
    </li>
</ul>

I need to grab the <span class="arrow up"> values as this is the service status, and the <li class="status service3"> as this tells me what service it is.

Since my status page API uses IDs and not "up/down" etc I will need the statuses and services to be in arrays so I can convert them into my formats.

$xml = new SimpleXMLElement($html);

$result = $xml->xpath('//ul/li');
$up = array(); //array of servises that is 'up'
$down = array(); //array of servises that is 'down'
foreach ($result as $item)
{
    if (strpos($item->span[1]->attributes()->class, 'up') !== false)
    {
        $up[] = str_replace("status ", "", $item->attributes()->class);
    }

    if (strpos($item->span[1]->attributes()->class, 'down') !== false)
    {
        $down[] = str_replace("status ", "", $item->attributes()->class);
    }
}

var_dump($up);
var_dump($down);

Will output for "Up" system status

array(3) {
  [0]=>
  string(8) "service1"
  [1]=>
  string(8) "service2"
  [2]=>
  string(8) "service3"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM