0

I'm scraping a page with table contents like such:

<table border="1" style="width:100%;" cellspacing="0" cellpadding="3">
<tr class="stats-section">
<td colspan="99">Scoring</td>
</tr>
<tr class="stats-section">
<td colspan="99">2nd Period</td>
</tr>
<tr class="hscore">
<td>UMD</td>
<td>4&#215;4</td>
<td>
Kobe Roth (1)</td>
<td>                Noah Cates, Casey Gilling           </td>
<td align="right">12:35</td>
</tr>
<tr class="vscore">
<td>BSU</td>
<td>4&#215;4</td>
<td>
Alex Ierullo (1)</td>
<td>                Kyle Looft          </td>
<td align="right">13:06</td>
</tr>
<tr class="stats-section">
<td colspan="99">3rd Period</td>
</tr>
<tr class="hscore">
<td>UMD</td>
<td></td>
<td>
Blake Biondi (1)</td>
<td>                Quinn Olson         </td>
<td align="right">10:10</td>
</tr>
</table>

I want to match the <tr class="hscore" AND the <tr class="vscore and change them into the following:

<tr class="hscore">
<td>#1 UMD</td> <!--added #1 -->
...
<tr class="vscore">
<td>#2 BSU</td> <!-- added #2 -->
...
</tr></table>

I don't know what order, or even how many of each hscore or vscore entries there will be. I need to auto increment a variable ($i++;) upon each match to echo the #1 and #2. Is regex my best bet? Maybe str_replace or is Simple Dom and a foreach of each better?

I can't think of a way to add to the $i variable on each match.

  • 1
    Here is really good answer for you [Check this out. Its my favorite](https://stackoverflow.com/a/1732454/9026411) – Roman Krut Nov 02 '21 at 18:45

1 Answers1

0

Use xpath.

$xml = simplexml_load_string($html);

$trs = $xml->xpath("//tr[@class='hscore']");
$i = 1;
foreach ($trs as $tr) {
    $tr->td[0] = "#$i ".$tr->td[0];
    ++$i; 
}

$trs = $xml->xpath("//tr[@class='vscore']");
$i = 1;
foreach ($trs as $tr) {
    $tr->td[0] = "#$i ".$tr->td[0];
    ++$i; 
}

echo '<pre>' . htmlentities($xml->asXML()) . '<?pre>';

gives

<?xml version="1.0"?>
<table border="1" style="width:100%;" cellspacing="0" cellpadding="3">
<tr class="stats-section">
<td colspan="99">Scoring</td>
</tr>
<tr class="stats-section">
<td colspan="99">2nd Period</td>
</tr>
<tr class="hscore">
<td>#1 UMD</td>
<td>4&#xD7;4</td>
<td>
Kobe Roth (1)</td>
<td>                 Noah Cates, Casey Gilling            </td>
<td align="right">12:35</td>
</tr>
<tr class="vscore">
<td>#1 BSU</td>
<td>4&#xD7;4</td>
<td>
Alex Ierullo (1)</td>
<td>                 Kyle Looft            </td>
<td align="right">13:06</td>
</tr>
<tr class="stats-section">
<td colspan="99">3rd Period</td>
</tr>
<tr class="hscore">
<td>#2 UMD</td>
<td/>
<td>
Blake Biondi (1)</td>
<td>                 Quinn Olson            </td>
<td align="right">10:10</td>
</tr>
</table>