Jump to content

preg_match() not listing first match? - Assistance needed


Anidazen

Recommended Posts

Hello.

I am running a regular expression with preg_match_all(). All expressions match, but for some reason $matches[1][0] is empty. (The first match.) When tested in a reg_ex editor for group1 matches, the first number DOES appear. I think the expression is valid but somehow PHP isn't retrieving that info!

Here is the expression I'm using:
  preg_match_all('/Gold ([0-9]?\,?[0-9]*).*?\$([0-9]*\.[0-9]*)/s', $content, $matches);

Here is the code on which I'm using it:
[quote] <td class="column1">
<input type="submit" name="CurList:CurTable:_ctl1:Button1" value="Add to Cart" id="CurList_CurTable__ctl1_Button1" class="add" />
Gold 250
<br />
<span class="sale">
$20.78
</span>
<br />

</td>
</tr>

<tr>
<td class="column1">
<input type="submit" name="CurList:CurTable:_ctl2:Button1" value="Add to Cart" id="CurList_CurTable__ctl2_Button1" class="add" />
Gold 500
<br />
<span class="sale">
$39.57
</span>
<br />

</td>
</tr>

<tr>
<td class="column1">
<input type="submit" name="CurList:CurTable:_ctl3:Button1" value="Add to Cart" id="CurList_CurTable__ctl3_Button1" class="add" />
Gold 1,000
<br />
<span class="sale">
$77.15
</span>
<br />

</td>
</tr>

<tr>
<td class="column1">
<input type="submit" name="CurList:CurTable:_ctl4:Button1" value="Add to Cart" id="CurList_CurTable__ctl4_Button1" class="add" />
Gold 1,500
<br />
<span class="sale">
$112.23
</span>
<br />

</td>
</tr>

<tr>
<td class="column1">
<input type="submit" name="CurList:CurTable:_ctl5:Button1" value="Add to Cart" id="CurList_CurTable__ctl5_Button1" class="add" />
Gold 2,000
<br />
<span class="sale">
$146.31
</span>
<br />

</td>
</tr>

<tr>
<td class="column1">
<input type="submit" name="CurList:CurTable:_ctl6:Button1" value="Add to Cart" id="CurList_CurTable__ctl6_Button1" class="add" />
Gold 3,000
<br />
<span class="sale">
$216.47
</span>
<br />

</td>
</tr>

<tr>
<td class="column1">
<input type="submit" name="CurList:CurTable:_ctl7:Button1" value="Add to Cart" id="CurList_CurTable__ctl7_Button1" class="add" />
Gold 4,000
<br />
<span class="sale">
$282.63
</span>
<br />

</td>
</tr>

</table>[/quote]


For some reason, the output I get is 100% correct, but always misses the first 250! $matches[1][0] is empty but $matches[2][0] is correct with the corresponding figure!

Here's the code to output (I've checked outside of this code -- $matches[1][0] is somehow empty. So It's not this that's at fault).

$i = 0;
$matchlength = sizeof($matches[0]);
while($i < $matchlength){
echo $matches[1][$i] . " gold: $" . $matches[2][$i] . "<br>";
$i++;
}


Here is the output I am getting:

gold: $20.78
500 gold: $39.57
1,000 gold: $77.15
1,500 gold: $112.23
2,000 gold: $146.31
3,000 gold: $216.47
4,000 gold: $282.63
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.