简体   繁体   English

PHP DOMXPath-无法定位正确的节点

[英]PHP DOMXPath - Can't target the right node

I know this is probably covered in other threads, but I've been searching all over StackOverflow and tried many solutions, this is why I'm asking. 我知道这可能在其他线程中涵盖了,但是我一直在StackOverflow上进行搜索并尝试了许多解决方案,这就是我要问的原因。

With this html: 与此HTML:

<div class="someclass">
<table>
    <tbody>
        <tr>
            <th class="state">Status</th>
            <th class="name">Name</th>
            <th class="type">Type</th>
            <th class="length">Length</th>
            <th class="height">Height</th>
        </tr>
        <tr>
            <td class="state state2"></td>
            <td class="name"></td>
            <td class="type t18"></td>
            <td class="length">2000 m</td>
            <td class="height"></td>
        </tr>
        <tr>
            <td class="state state1"></td>
            <td class="name"></td>
            <td class="type t18"></td>
            <td class="length">2250 m</td>
            <td class="height"></td>
        </tr>
        <tr>
            <td class="state state1"></td>
            <td class="name"></td>
            <td class="type t18"></td>
            <td class="length">3000 m</td>
            <td class="height"></td>
        </tr>
        <tr>
            <td class="state state2"></td>
            <td class="name"></td>
            <td class="type t18"></td>
            <td class="length">2250 m</td>
            <td class="height"></td>
        </tr>
    </tbody>
</table>
</div>

Now, this is the PHP code I have so far : 现在,这是我到目前为止拥有的PHP代码:

$dom = new DOMDocument();
$dom->loadHtmlFile('http://www.whatever.com');
$dom->preserveWhiteSpace = false;

$xp = new DOMXPath($dom);
$col = $xp->query('//td[contains(@class, "state1") and (contains(@class, "state"))]');
$length = 0;

foreach( $col as $n ) {
    $parent = $n->parentNode;
    $length += $parent->childNodes->item(3)->nodeValue; 
}
echo 'Length: ' . $length;

I need to: 我需要:

1.- Sum the 'length' values so I can echo them, getting rid of the ' m' substring of the given values . 1.- 对“长度”值求和,以便我可以回显它们, 摆脱给定值的“ m”子串

2.- Understand why I'm getting wrong the 'parentNodes', 'childNodes' and 'item()' parts . 2.-了解为什么我弄错了'parentNodes','childNodes'和'item()'部分 With many tries I've gotten 'Length: 0' 经过多次尝试,我得到了“长度:0”

I know this isn't the place to get a full detailed explanation, but it is really hard to find tutorials targetting these concrete issues. 我知道这里不是获得完整详细说明的地方,但是很难找到针对这些具体问题的教程。 It would be great if someone could give some advice on where I can get this information. 如果有人可以就我可以从何处获得此信息提供建议,那将是很好的。

Thanks very much in advance. 首先十分感谢。

Edited the 'Concat' part for simplicity. 为简单起见, 编辑了“ Concat”部分。

Navigation through DOMDocument for a specified childNode value by using DOMXpath 使用DOMXpath在DOMDocument中导航指定的childNode值

function getInt($string)
{
    preg_match("/[0-9]+/i", $string, $val);

    $out = 0;
    if (isset($val) && !empty($val))
    {
        $out = $val[0];
    }

    return intval($out);
}

$dom = new DOMDocument();
$dom->loadHtml($html);
$dom->preserveWhiteSpace = false;

$xp = new DOMXPath($dom);
$length = 0;

foreach($xp->query('//td[@class="state state1"]/following-sibling::*[3]') as $element)
{
    $value = $element->nodeValue;
    $length += getInt($value);
}


echo $length;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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