0

How do I cut out only the first <tr class="ismResult"> and <tr class="ismFixtureSummary"> in this, but not the second set?

I am looking to cut out only the first two <tr> tags and contents. Is there a way to set both of these to a variable eg, $Result? I'm looking to keep the html as part of it.

<table>
    <tbody>
        <tr class="ismResult">
            <td>2-0</td>
        </tr>

        <tr class="ismFixtureSummary">
            <td>Player1</td>
            <td>Player2</td>
        </tr>

         <tr class="ismResult">
            <td>1-1</td>
        </tr>

        <tr class="ismFixtureSummary">
            <td>Player3</td>
            <td>Player4</td>
        </tr>
    </tbody>
</table>

WHAT I'VE TRIED:

include('simple_html_dom.php');
$url = 'http://www.test.com';
$html = file_get_html($url);

$FullTable = $html->find('table');

foreach($FullTable->find('tr[class=ismResult]') as $Heading)
    {
    echo $Heading;

    foreach($FullTable->find('tr[class=ismFixtureSummary]') as $Summary)    
    {
        echo $Summary;
    }
    }

This doesn't work because it posts the contents of all <tr class="ismFixtureSummary"> into every <tr class="ismResult">. I am trying to cut them out as a pair.

Thanks for any help you can give.

Cully
  • 484
  • 9
  • 19

2 Answers2

0

I would tackle this using XPATH

$xpath = new DOMXPath($html);

$ismResult = $xpath->query("//tr[@class='ismResult']/td");
$ismFixtureSummary= $xpath->query("//tr[@class='ismFixtureSummary']/td");

echo $ismResult->item(0)->nodeValue;
echo $ismFixtureSummary->item(0)->nodeValue;

You can read more about xpath here. http://phpmaster.com/php-dom-using-xpath/

Joseph
  • 238
  • 1
  • 9
  • Xpath looks useful. The only problem I have with this is that I'll have to go and learn something from the start again. I'll give it a go, but it'll take a while before I know if it works for me or not. Thanks a lot for the suggestion. – Cully Nov 01 '12 at 04:58
0

Just set the results of your find operation into two separate arrays and then just do a single for loop and echo the results you are looking for.

include('simple_html_dom.php');
$url = 'http://www.test.com';
$html = file_get_html($url);

$FullTable = $html->find('table');

$headings = $FullTable->find('tr[class=ismResult]');
$summaries = $FullTable->find('tr[class=ismFixtureSummary]');

for ($i = 0; $i < count($headings); $i++;) {
    echo $headings[$i] . $summaries[$i];
}
Mike Brant
  • 70,514
  • 10
  • 99
  • 103
  • Thanks, this gets me further along than I've been and will work, however the while loop doesn't work on my side. If I echo out stuff outside the while loop it works, but not inside it. – Cully Nov 01 '12 at 05:10
  • I got it to work.. I put the `$i=0;` before the while, and `$i++;` inside the loop, after the echo line. Thanks again for your help! – Cully Nov 01 '12 at 05:19