質問

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! :(

役に立ちましたか?

解決

Find the first TR containing $needle

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

Find the first TR containing $needle, and if neested, the TR closes to the needle. that can be solved by just repating the process.

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

他のヒント

In phpquery you would:

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

to get the TD:s of the first TR, you can use

$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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top