PHP DOM使用相同的属性访问对象

时间:2012-02-23 20:17:07

标签: php regex dom domdocument

我想获取日期对象文本内容和Team 1.但是Team 2对象与date对象具有相同的属性选项。我怎样才能获得正确的内容?如果我回显$ date,我会得到Team2的日期值...我应该怎么写条件?

<table width="100%" cellpadding=2 cellspacing=0 id="tblFixture" border=0>
    <tr class=row1 align=center side='home'>
        <td align=left>21.09.1928</td>
        <td>&nbsp;</td>
        <td align='right'><span class='team'>Team 1</span></td>
        <td align=left><a href='http://www.foo.com/bar' target='_blank'>Team 2</a></td>
        </td>
    </tr>

PHP代码:

$url = "http://www.bla.com/bla.html";
$dom = new DOMDocument;
@$dom->loadHTMLFile($url);
$xpath = new DOMXPath($dom);
$nlig  = $xpath->query('//table[@id="tblFixture"]/tr[@side=\'home\']');

$i = 0;
foreach ($nlig AS $val)
{
    $date       = $xpath->query('//table[@id="tblFixture"]/tr[@side=\'home\'][@class=\'row1\']/td[@align=\'left\']')->item($i)->textContent;
    $first_team = $xpath->query('//table[@id="tblFixture"]/tr[@side=\'home\']/td[@align=\'right\']/span[@class=\'team\']')->item($i)->textContent;

    echo $date, $first_team, "<br />";
    $i++;
}

1 个答案:

答案 0 :(得分:3)

您可以使用正则表达式来验证/查找日期。

类似的东西:

preg_match("/<td align=left>([0-9]{2}.[0-9]{2}.[0-9]{4})<\/td>/", $html, $matches);