Jump to content


Photo

preg_match() not listing first match? - Assistance needed


  • Please log in to reply
1 reply to this topic

#1 Anidazen

Anidazen
  • Members
  • PipPipPip
  • Advanced Member
  • 79 posts

Posted 13 July 2006 - 03:13 PM

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:

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



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

#2 effigy

effigy
  • Staff Alumni
  • Advanced Member
  • 3,600 posts
  • LocationIL

Posted 13 July 2006 - 03:36 PM

This matches 250 for me. What's your PHP version?
Regexp | Unicode Article | Letter Database
/\A(e)?((1)?ff(?:(?:ig)?y)?|f(?:ig)?)\z/




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users