...

View Full Version : preg_match(_all)



AoR Zeta
01-18-2008, 01:09 AM
I'm using preg_match and preg_match_all on the contents of a file I retrieved using file_get_contents. Sometimes it works fine and sometimes it just refuses to work.

Say the source of the web page has


<tr>
<td>Played</td>
<td class="values">778</td>
</tr>
<tr>
<td class="col1 left">Earned</td>
<td class="col2">49 of 49</td>
</tr>


I do a preg_match


<?php preg_match('/<td class="values">(.*?)<\/td>/', $file, $result); ?>

and it works fine but then I do another


<?php preg_match('/<td class="col2">(.*?)<\/td>/', $file, $result); ?>

and it doesn't work.

Anyone have any ideas? The problem happens with both preg_match and preg_match_all, sometimes when I search for something it works fine and other times it doesn't... it's really annoying. Thanks a lot.

oesxyl
01-18-2008, 07:19 AM
Anyone have any ideas? The problem happens with both preg_match and preg_match_all, sometimes when I search for something it works fine and other times it doesn't... it's really annoying.


<?php preg_match('/<td class="col2">([^<]*)<\/td>/', $file, $result); ?>
but this select only the elements wiyh class="col2'

to select td for any class


<?php preg_match('/<td class="[^"]+">([^<]*)<\/td>/', $file, $result); ?>

check also the semantics for arg 2 and 3 of the preg_match against what you want/have in $file and $result.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum