Jump to content

DOM question


Cyto

Recommended Posts

    /*** a new dom object ***/
    $dom = new domDocument;

    /*** load the html into the object ***/
    @$dom->loadHTML($file);

    /*** discard white space ***/
    $dom->preserveWhiteSpace = false;

    /*** the table by its tag name ***/
    $tables = $dom->getElementsByTagName('table');

    /*** get all rows from the table ***/
    $rows = $tables->item(0)->getElementsByTagName('tr');

    /*** loop over the table rows ***/
    foreach ($rows as $row)
    {
        /*** get each column by tag name ***/
        $cols = $row->getElementsByTagName('a');
        /*** echo the values ***/
        echo $cols->item(0)->nodeValue.'<br />';
    } 

 

Hi, Is there a way to start to echo from 43?

thx.

Link to comment
https://forums.phpfreaks.com/topic/251921-dom-question/
Share on other sites

I don't work with DOM elements in PHP. I would think there would be a way to get the count of elements in the object but couldn't find a way. If so, that would be the best option as you could simply start at whatever element you wanted. but, this will work. Simply skip the first n elements.

 

    /*** loop over the table rows ***/
    $startRow = 48; // 49th row
    $rowNo = 0;
    foreach ($rows as $row)
    {
        if($rowNo>=$startRow)
        {
            /*** get each column by tag name ***/
            $cols = $row->getElementsByTagName('a');
            /*** echo the values ***/
            echo $cols->item(0)->nodeValue.'<br />';
        }
        $rowNo++;
    }

Link to comment
https://forums.phpfreaks.com/topic/251921-dom-question/#findComment-1291746
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.