-1

I got head-ache trying to solve this problem. I have a structure like this:

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

this format will repeat many times with different text contain, sometime, some text contain is similar. I need to extract ONLY the FIRST group of this format, where it contain "ABC Football Club" the FIRST TIME (because it could appear many times later also). How do I do that and extract the text on each line ?

Thanks for the comments, I editted here to add some codes I tried:

    $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);

However this one not work! :(

4

3 回答 3

2

找到第一个包含 $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;
于 2013-08-13T09:55:39.677 回答
1

在 phpquery 中,你会:

$dom = phpQuery::newDocument($html);
$dom->find('tr:has(> td:contains("ABC Football Club"))')->eq(0);
于 2013-08-14T23:52:21.220 回答
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;
}
于 2013-08-13T09:41:26.393 回答