0

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.

With this 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 :

$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.

2.- Understand why I'm getting wrong the 'parentNodes', 'childNodes' and 'item()' parts. With many tries I've gotten 'Length: 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.

11
  • You have a syntax error on the line where you do the query. Commented Jan 18, 2016 at 12:48
  • Thanks, could you tell me what's that syntax error? Can't see it. Commented Jan 18, 2016 at 12:53
  • You did not escape the ' in your concat function. Commented Jan 18, 2016 at 12:54
  • Sorry @Musa, ovbiously a beginner here... I still don't see the error. Commented Jan 18, 2016 at 12:57
  • Inside concat you have ' ' , to use single quotes in a single quoted string you have to escape it with a \ (slash) Commented Jan 18, 2016 at 13:02

1 Answer 1

0

Navigation through DOMDocument for a specified childNode value by using DOMXpath

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;
Sign up to request clarification or add additional context in comments.

15 Comments

thanks. Could you explain that in some detail please?
for the given DOM you provide it get the values 3000 m and 2250 m from the node then pass to getInt() function to get the int part from string and return int value one by one in this case two values and add the return value with the current $length value ...
Please check eval.in/504064 Notice I've filled every td with values and the results seem to match and sum the first td's for every tr, as per your exact code.
it seems it access all child nodes values ok let me dig it more
It sums all lengths, doesn't discriminate between 'state1' and 'state2', etc. Check eval.in/504086
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.