Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8262131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:39:47+00:00 2026-06-08T03:39:47+00:00

I am using domDocument. I am close but need help for the last little

  • 0

I am using domDocument. I am close but need help for the last little bit

I have this html just a snippet below.
There are a number of rows. I am trying to get the href.

so far i am doing the following:
I can get the table, tr, and td ok , but not sure what to do from there.

Thanks for any help

foreach ($dom->getElementsByTagName('table') as $tableitem) {
    if ( $tableitem->getAttribute('class') == 'tableStyle02'){
        $rows = $tableitem->getElementsByTagName('tr');
        foreach ($rows as $row){ 
            $cols = $row->getElementsByTagName('td'); 

            $hrefs = $cols->item(0)->getElementsByTagName('a'); 
        }     
    }
}

html snippet:

<table width="100%" border="0" cellspacing="0" cellpadding="2" class="tableStyle02"> 
    <tr> 
        <td><span class="Name"><a href="bin.php?cid=703&size=0">
               <strong>Conference Facility</strong></a></span></td>
        <td align="center" nowrap>0.00</td>
        <td align="center">&nbsp;0&nbsp;</td>
        <td align="center">&nbsp;&nbsp;</td>
        <td align="center">&nbsp;0&nbsp;</td>
        <td align="center">&nbsp;0&nbsp;</td>
        <td align="center">&nbsp;0 - 0 &nbsp;</td>
        <td align="center">&nbsp;Wired Internet,&nbsp;&nbsp;&nbsp;</td>
        <td align="center">&nbsp;&nbsp;</td>
    </tr>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T03:39:48+00:00Added an answer on June 8, 2026 at 3:39 am

    Let me introduce you the concept of xpath, a query language for DomDocuments:

    //table[@class="tableStyle02"]//a/@href
    

    Reads as: Take the table tag with class attribute tableStyle02 and then the href attribute from within the a child tag.

    Or as you had the foreach for tr and td elements as well:

    //table[@class="tableStyle02"]/tr/td/a/@href
    

    Now in that path, the a tag is a direct children of the td tag which is a direct children of the tr tag which is a direct children of the table tag. As you can see, with xpath it is much easier to formulate the path to the element than writing everything in PHP code.

    Apropos PHP code, in PHP this can look like:

    $doc = new DOMDocument();
    $doc->loadHTML($html);
    $xp = new DOMXPath($doc);
    $href = $xp->evaluate('string(//table[@class="tableStyle02"]//a/@href)');
    

    The variable $href then contains the string: bin.php?cid=703&size=0.


    This example is with a string (string(...)), so ->evaluate returns a string, which is created from the first found attribute node. Instead you can return a nodelist as well:

    $hrefs = $xp->query('//table[@class="tableStyle02"]/tr/td/span/a/@href');
    #             ^^^^^                                       ^^^^
    

    Now $hrefs contains the usual DOMNodeList, here it contains all the href attribute nodes:

    echo $hrefs->item(0)->nodeValue; # bin.php?cid=703&size=0
    

    Take care that if you use only one slash / to separate tags, that they need to be direct children. With two slashes // it can be a descendant (child or child of child (of child (of …))).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse some HTML using DOMDocument, but when I do, I suddenly
I'm just getting started with using php DOMDocument and am having a little trouble.
I'm just getting started with using php DOMDocument and am having a little trouble.
With this code using DomDocument: <?php $html = '<pre>one</pre><pre>two</pre><pre>three</pre><pre>four</pre>'; $doc = new DomDocument(); $doc->loadHTML($html);
I am trying to learn using DOMDocument for parsing HTML code. I am just
I am having issues removing a node using PHP DomDocument. I have some HTML
I have the following html and i am using php's DomDocument class to get
I'm using DOMDocument to generate a XML and this XML has to have an
I'm using PHP's DOMDocument to parse and normalize user-submitted HTML using the loadHTML method
I'm using a WebBrowser control in design mode. webBrowser1.DocumentText = <html><body></body></html>; doc = webBrowser1.Document.DomDocument

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.