douqian1975 2016-05-30 13:19
浏览 160
已采纳

将html标签转换为php数组

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.

  • 写回答

1条回答 默认 最新

  • dqwcdqs358367 2016-05-30 13:44
    关注
    $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"
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果