1

I am using some code to pick out all the <td> tags from a HTML page:

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('td') as $node) {
$array_data[ ] = $node->nodeValue;
}

This stores the data fine in my array.

The html data being looked at is:

<tr>
<td>DATA 1</td>
<td><a href="12345">DATA 2</a></td>
<td>DATA 3</td> 
</tr>

The $array_data returns:

Array([0])=>DATA 1 [1]=>DATA 2 [2]=> DATA 3)

My desired output is to get code out of the <a> tag that is associated with the on the page. Desired output:

Array([0])=>DATA 1 [1]=>12345 [2]=>DATA 2 [3]=> DATA 3)

I think <a> would be called child node, I am very new to working with DOM sorry if this seems a stupid question.

I have read SO link: Using PHP dom to get child elements

I've used this code to pick out the href:

   foreach ($dom->getElementsByTagName('td') as $node) {
      foreach ($node->getElementsByTagName('a') as $node){
      $link = $node->getAttribute('href');
      echo '<br>';
      echo $link;
      }
      $array_data[ ] = $node->nodeValue;
   }

Any help or pointers for other reading material would be greatly appreicated!
Thanks

2
  • I posted answer to solving your problem. So why you changed question? What is your problem? Commented Apr 21, 2017 at 13:10
  • Thanks for your solution Mohammed. I was reading other SO posts to find a solution at the same time. Once I'd found something on the SO post I posted the updated code in my question. Commented Apr 21, 2017 at 13:20

1 Answer 1

5

You should check td has a child. Select anchor tag using getElementsByTagName() and check the selection has content using length property. If the td has anchor in child, use getAttribute() to get href attribute of it.

$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('td') as $node) {
    $nodeAnchor = $node->getElementsByTagName("a");
    if ($nodeAnchor->length)
        $array_data[] = $nodeAnchor->item(0)->getAttribute("href");
    $array_data[] = $node->nodeValue;
}

See demo

Sign up to request clarification or add additional context in comments.

Comments

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.