我想php代码找到href标题和其他一些信息从html表


I want php code to find href title and some other infos from html table

我创建了下面的代码:

<?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');
}
?>

我有html页面与表,所以我想链接的标题和日期。html代码示例:

<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>

这个链接对我来说很好,但是我不知道怎么拿其他的。

Tnx .

你在哪里做这件事:

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

你正在拿回所有的A标签。只有一个。

如果你想获得文本"THIS IS DATE",你不会通过查找A标签来获得它,因为文本不在A标签内-它在TD标签中。

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

…可以得到所有的TD元素,或者你可以为你想要的目标元素指定一个ID,并使用getElementById代替。

基本上,这些信息都在文档中,你绝对应该在问问题之前阅读。阅读的快乐!

同样是:http://php.net/manual/en/class.domdocument.php