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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.