简体   繁体   English

我可以将 domxpath 嵌套类的结果放入带有键 => 值的数组中吗?

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

I'm getting some data from a webpage for clients and that works fine, it gets all data in seperate rows by exploding the \n into new lines which I then map to specific array data to fill form fields with.我从客户端的网页中获取一些数据并且工作正常,它通过将 \n 分解为新行来获取单独的行中的所有数据,然后我将 map 到特定的数组数据以填充表单字段。 Like so for each needed value:对于每个需要的值,就像这样:

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

However, the data i need may be in Line 10 today, but might very well be line 11 tomorrow.但是,我需要的数据今天可能在第 10 行,但明天很可能在第 11 行。 So I'd like to get the values into named arrays.所以我想将这些值放入名为 arrays 的值中。 A sample of the HTML on the URL is as follows: 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>

This is my function model:这是我的 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;
}

What am I getting?我得到了什么?

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

Desired output anything like:所需的 output 类似:

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

Is there a way to "loop" through the itemList and get each itemName and itemValue into an associative array?有没有办法“循环”通过 itemList 并将每个 itemName 和 itemValue 放入关联数组中?

If your happy with what the getShortTags() method does (or if it's used elsewhere and so difficult to tweak), then you can process the return value.如果您对getShortTags()方法的作用感到满意(或者如果它在其他地方使用并且很难调整),那么您可以处理返回值。

This code first uses explode() to split the output by line, uses array_map() and trim() to remove any spaces etc., then passes the result through array_filter() to remove blank lines.此代码首先使用explode()将output 按行拆分,使用array_map()trim()删除任何空格等,然后将结果传递给array_filter()以删除空白行。 This will leave the data in pairs, so an easy way is to use array_chunk() to extract the pairs and then foreach() over the pairs with the first as the key and the second as the value...这将使数据成对保留,因此一种简单的方法是使用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);

with the sample data gives..与样本数据给出..

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

To use this directly in the document and without making any assumptions (although if you don't have a name for several values, then not sure what you will end up with).直接在文档中使用它而不做任何假设(尽管如果您没有多个值的名称,那么不确定最终会得到什么)。 This just looks specifically for the base element and then loops over the <span> elements.这只是专门查找基本元素,然后循环遍历<span>元素。 Each time within this it will look for the itemName and itemValue class attributes and get the value from these...每次在其中它都会查找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