在重复<;tr></tr>;标签


Get text between repeating <tr></tr> tags

我在试图解决这个问题时头疼。我有一个这样的结构:

<tr>
<td width="10%" bgcolor="#FFFFFF"><font class="bodytext9">17-Aug-2013</font></td>
<td width="4%" bgcolor="#FFFFFF" align=center><font class="bodytext9">Sat</font></td>
<td width="4%" bgcolor="#FFFFFF" align="center"><font class="bodytext9">5 PM</font></td>
<td width="15%" bgcolor="#FFFFFF" align="center"><a class="black_9" href="teams.asp?teamno=766&leagueNo=115">XYZ Club FC</a></td>
<td width="5%" bgcolor="#FFFFFF" align="center"><font class="bodytext9"><img src="img/colors/white.gif"></font></td>
<td width="5%" bgcolor="#FFFFFF" align="center"></td>
<td width="5%" bgcolor="#FFFFFF" align="center"><font class="bodytext9">vs</font></td>
<td width="5%" bgcolor="#FFFFFF" align="center"></td>
<td width="5%" bgcolor="#FFFFFF" align="center"><font class="bodytext9"><img src="img/colors/orange.gif"></font></td>
<td width="15%" bgcolor="#FFFFFF" align="center"><a class="black_9" href="teams.asp?teamno=632&leagueNo=115">ABC Football Club</a></td>
<td width="15%" bgcolor="#FFFFFF" align="center"><a href="pitches.asp?id=151" class=list><u>APSM Pitch </u></a></td>
<td width="4%" bgcolor="#FFFFFF" align="center"><a target="_new" href="matchpreview_frame.asp?matchno=20877"><img src="img/matchpreview_symbol.gif" border="0"></a></td>
</tr>

这种格式会重复多次不同的文本包含,有时,某些文本包含是相似的。我只需要提取这个格式的第一组,其中第一次包含"ABC足球俱乐部"(因为它以后也可能出现很多次)。我该如何做到这一点并提取每一行的文本?

感谢您的评论,我在这里编辑添加了一些我尝试过的代码:

    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'url link');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);                            
$xpath = new DOMXPath($dom);
$trs = $xpath->query('//tr/td[contains(.,'ABC Football Club')]');
$rows = array();
foreach($trs as $tr)
   $rows[] = innerHTML($tr, true); // this function I don't include here
print_r($rows);

然而,这个不起作用!:(

查找包含$needle 的第一个TR

$needle = "ABC Football Club";
$doc = new DOMDocument();
$doc->loadHTML($html);
$trs = $doc->getElementsByTagName('tr');
foreach($trs as $current_tr)
{
   $tr_content = $doc->saveXML($current_tr);
   if(strpos($tr_content, $needle) !== FALSE)
   {
      break;
   }
   else
   {
      $tr_content= "";
   }
}
echo $tr_content;

找到包含$needle的第一个TR,并且如果需要,TR靠近针。这可以通过重新安排流程来解决。

$needle = "ABC Football Club";
$doc = new DOMDocument();
$doc->loadHTML($html);
$node = $doc;
do
{
    $trs = $node->getElementsByTagName('tr');
    $node = NULL;
    foreach($trs as $current_tr)
    {
       $tr_content = $doc->saveXML($current_tr);
       if(strpos($tr_content, $needle) !== FALSE)
       {
          $node = $current_tr;
          $found_tr = $node;
          $found_tr_content = $tr_content;
          break;
       }
    }
} while($node);
echo $found_tr_content;

在phpquery中,您可以:

$dom = phpQuery::newDocument($html);
$dom->find('tr:has(> td:contains("ABC Football Club"))')->eq(0);

要获得第一个TR的TD:s,可以使用

$doc = new DOMDocument();
$doc->loadHTML($html);
$trs = $doc->getElementsByTagName('tr');
$td_of_the_first_tr = $trs->item(0)->getElementsByTagName('td');
foreach($td_of_the_first_tr as $current_td)
{
   echo $doc->saveXML($current_td) . PHP_EOL;
}