Menu

#54 Traversing the Dom within a series of columns

v1.0_(example)
closed
None
1
2022-04-09
2020-09-16
Daniel
No

Hi all, love this tool and thanks for putting this together. I have the following HTML:

A B Description of Link I'm trying to combine the 2nd and 3rd columns and also to pull the href link, along with the description. I am having trouble figuring out how to grab the "href" and the "Description of Link" I would appreciate if you could share how I can traverse the DOM to pull the href and the contents of the tag. In trying to use children() and next_sibling() I am not able to get the solution. Thank you.

Discussion

  • LogMANOriginal

    LogMANOriginal - 2022-04-09
    • status: open --> closed
    • assigned_to: LogMANOriginal
     
  • LogMANOriginal

    LogMANOriginal - 2022-04-09

    You probably figured it out in the mean time, but here is a complete example that will give you what you want.

    <?php
    
    include_once 'simple_html_dom.php';
    
    $doc = <<<EOD
    <tr>
        <td></td>
        <td  id="column2" class="style3">A</td>
        <td id="column2" class="style2">B</td>
        <td>
            <a href="#link")>Description of Link</a>
        </td>
    </tr>
    EOD;
    
    
    $html = str_get_html($doc);
    
    
    $href = $html->find('a', 0)->href;
    $description = $html->find('a', 0)->innertext;
    
    echo $href . PHP_EOL . $description . PHP_EOL;
    
    // #link
    // Description of Link
    

    You can also find the docs for this library at https://simplehtmldom.sourceforge.io/

     

Log in to post a comment.

MongoDB Logo MongoDB