使用 DOM XPath 将内容获取到数组中

Get content into array using DOM XPath

我正在尝试将标题、信息和时间放入数组中。但是,我坚持获取数据。正如您在我的控制器中看到的那样,我使用 getelementbyID 来调用我的 first-list,但它向我显示了 DOMElement Object。谁能帮忙? :/

HTML :

<div class="col-sm-7 tracking">
    <div class="box">
       <ul id="first-list"> 
        <li>
           <span></span>
                 <div class="title">BKI</div>
                 <div class="info">SHIPMENT DELIVERED</div>
                        <div class="time">
                            <span>01/May/2020</span>
                            <span style="color:grey">9:05PM</span>
                        </div>
        </li>

        <li>
           <span></span>
                  <div class="title">BKI</div>
                  <div class="info">SHIPMENT OUT FOR DELIVERY</div>
                      <div class="time">
                            <span>01/May/2020</span>
                            <span style="color:grey">8:56PM</span>
                        </div>
        </li>
    </ul>
 </div>

控制器:

$DOM = new DOMDocument;
    libxml_use_internal_errors(true);
    $DOM->loadHTML($response);

    $xpath = new DOMXPath($DOM);
    $tbody = $DOM->getElementsByTagName('col-sm-7 tracking');

    $query = '//li';

    $entries = $xpath->query($query);

    foreach ($entries as $entry) {
        echo $entry->nodeValue . "<br>";
    }

正在尝试构建如下数组:

[0] => Array
        (
            [Title] => Text 1
            [Info] => Text 1
            [Time] => Text 1
        )

    [1] => Array
        (
            [Title] => Text 2
            [Info] => Text 2
            [Time] => Text 2
        )

这使用 XPath 查找每个 <li> 标签下的相关部分,以提取各种信息。要记住的是使用 <li> 作为后续表达式的起点(将其作为第二个参数传递给 XPath 调用)。

有些调用您只需要我使用的文本 evaluate() 所以它节省了额外的步骤来获取文本...

foreach ($entries as $entry) {
    $title = $entries = $xpath->evaluate('string(div[@class="title"])', $entry);
    $info = $entries = $xpath->evaluate('string(div[@class="info"])', $entry);
    $timeParts = $entries = $xpath->query('div[@class="time"]/span', $entry);
    $time = [];
    foreach ( $timeParts as $part ) {
        $time[] = $part->textContent;
    }
    $output[] = [ "title" => $title, "info" => $info, 
        "time" => implode(" ", $time) ];
}