簡體   English   中英

我可以將 domxpath 嵌套類的結果放入帶有鍵 => 值的數組中嗎?

[英]Can I get the result of domxpath nested classes into an array with keys => value?

我從客戶端的網頁中獲取一些數據並且工作正常,它通過將 \n 分解為新行來獲取單獨的行中的所有數據,然后我將 map 到特定的數組數據以填充表單字段。 對於每個需要的值,就像這樣:

$lines = explode("\n", $html);
$data['vraagprijs']         = preg_replace("/[^0-9]/", "", $lines[5]);

但是,我需要的數據今天可能在第 10 行,但明天很可能在第 11 行。 所以我想將這些值放入名為 arrays 的值中。 URL上的HTML示例如下:

<div class="item_list">             
<span class="item first status">
    <span class="itemName">Status</span>                        
    <span class="itemValue">Sold</span>
</span>
<span class="item price">
    <span class="itemName">Vraagprijs</span>
    <span class="itemValue">389.000</span>
</span>
<span class="item condition">
    <span class="itemName">Aanvaarding</span>
    <span class="itemValue">In overleg</span>
</span>
...
</div>

這是我的 function model:

$tagName3   = 'div';
$attrName3  = 'class';
$attrValue3 = 'item_list';
$html       = getShortTags($tagName3, $attrName3, $attrValue3, $url); 

function getShortTags($tagName, $attrName, $attrValue, $url = "", $exclAttrValue = 'itemTitle') {

    $dom = $this->getDom($url);

    $html                 = '';
    $domxpath             = new \DOMXPath($dom);
    $newDom               = new \DOMDocument;
    $newDom->formatOutput = true;

    $filtered = $domxpath->query(" //" . $tagName . "[@" . $attrName . "='" . $attrValue . "']/descendant::text()[not(parent::span/@" . $attrName . "='" . $exclAttrValue . "')] ");
    $i        = 0;
    while ($myItem   = $filtered->item($i++)) {
        $node   = $newDom->importNode($myItem, true);
        $newDom->appendChild($node); 
    }
    $html = $newDom->saveHTML();
    return $html;
}

我得到了什么?

Status\nSold\nVraagprijs\n389.000\nIn overleg\n....

所需的 output 類似:

$html = array("Status" => "Sold", "Vraagprijs" => "389.000", "Aanvaarding" => "In overleg", ...)

有沒有辦法“循環”通過 itemList 並將每個 itemName 和 itemValue 放入關聯數組中?

如果您對getShortTags()方法的作用感到滿意(或者如果它在其他地方使用並且很難調整),那么您可以處理返回值。

此代碼首先使用explode()將output 按行拆分,使用array_map()trim()刪除任何空格等,然后將結果傳遞給array_filter()以刪除空白行。 這將使數據成對保留,因此一種簡單的方法是使用array_chunk()提取對,然后使用foreach()對第一個作為鍵,第二個作為值的對...

$html = getShortTags($tagName3, $attrName3, $attrValue3, $url);
$lines = array_filter(array_map("trim", explode(PHP_EOL, $html)));
$pairs = array_chunk($lines, 2);
$output = [];
foreach ( $pairs as $pair ) {
    $output[$pair[0]] = $pair[1];
}
print_r($output);

與樣本數據給出..

Array
(
    [Status] => Sold
    [Vraagprijs] => 389.000
    [Aanvaarding] => In overleg
)

直接在文檔中使用它而不做任何假設(盡管如果您沒有多個值的名稱,那么不確定最終會得到什么)。 這只是專門查找基本元素,然后循環遍歷<span>元素。 每次在其中它都會查找itemNameitemValue class 屬性並從中獲取值...

$output = [];
$filtered = $domxpath->query("//div[@class='item_list']/span");
foreach ( $filtered as $myItem )  {
    $name= $domxpath->evaluate("string(descendant::span[@class='itemName'])", $myItem);
    $value= $domxpath->evaluate("string(descendant::span[@class='itemValue'])", $myItem);
    $output[$name] = $value;
}
print_r($output);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM