Jump to content

PHP Simple Dom, Regex to Replace, and Auto-Incement a Variable


bschultz

Recommended Posts

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 <tr> better?

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

Thanks!

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

Then change the code. I've given you a start.

 

[EDIT]

What the hell!

$xml = simplexml_load_string($html);

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

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

image.png.94d46794ded1108135acb32fe8457e9f.png

Edited by Barand
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.