-3

I create this code until now:

<?php
$url=" SOME HTML URL ";
$html = file_get_contents($url);
$doc = new DOMDocument();
@$doc->loadHTML($html);
$tags = $doc->getElementsByTagName('a');
foreach ($tags as $tag) {
    echo $tag->getAttribute('href');
}
?>

I have html pages with tables so i want the link the title and the date. Example of html code:

<TR>
    <TD align="center" vAlign="top" bgColor="#ffffff" class="smalltext">3</TD>
    <TD  class="plaintext" ><a href="pdf/blahblah.pdf" target="_blank" class="link1style">THIS IS THE TITLE</a> </TD>
    <TD  align="center" class="plaintext" >THIS IS DATE</TD>
</TR>

It works fine for me for the link, but i don't know how to take the others.

Tnx.

Mohammad
  • 21,175
  • 15
  • 55
  • 84

1 Answers1

0

Where you are doing this:

$tags = $doc->getElementsByTagName('a');

You are getting back all the A tags. There only happens to be one.

If you want to get the text "THIS IS DATE", you're aren't going to get it by looking in A tags because the text is not inside an A tag - it is in a TD tag.

$tds = $doc->getElementsByTagName('td');

... would work to get all the TD elements, or you could assign an ID to the element you want to target and use getElementById instead.

Basically, though, this information is all in the documentation, which you absolutely should read before asking questions. Happy reading!

Once again, that's: http://php.net/manual/en/class.domdocument.php

Chris Baker
  • 49,926
  • 12
  • 96
  • 115