supermerc Posted July 10, 2009 Share Posted July 10, 2009 Hey I know nothing about regex and Im getting a page with curl with something that looks like this: <tr> <td align="center"> <img src="/images/crewup/off.gif"> </td><td style="padding-left:25px;" align="left">Drifters</td> <td><a href="profile.php?id=7929">HardBaLLz</a></td> <td align="center">65</td> /tr> <tr> <td align="center"><img src="/images/crewup/off.gif"></td> <td style="padding-left:25px;" align="left">Drifters</td> <td><a href="profile.php?id=22746">0ddBaLL</a></td> <td align="center">66</td> </tr> ETC.. What I basicly want to do is grab all the ones that have <td style="padding-left:25px;" align="left">Drifters</td> and get their id from <td><a href="profile.php?id=22746"> name their name from <td><a href="profile.php?id=22746">0ddBaLL</a> So from that one id have 22746 OddBaLL Does anyone know how to do this? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted July 10, 2009 Share Posted July 10, 2009 Yup: <?php $str = '<tr> <td align="center"> <img src="/images/crewup/off.gif"> </td><td style="padding-left:25px;" align="left">Drifters</td> <td><a href="profile.php?id=7929">HardBaLLz</a></td> <td align="center">65</td> /tr> <tr> <td align="center"><img src="/images/crewup/off.gif"></td> <td style="padding-left:25px;" align="left">Drifters</td> <td><a href="profile.php?id=22746">0ddBaLL</a></td> <td align="center">66</td> </tr>'; preg_match_all('~<td style="padding-left:25px;" align="left">Drifters</td>\s*<td><a href="profile\.php\?id=([^"]+)">([^<]*)<~', $str, $matches); ?> $matches[1] will be an array holding the IDs, and $matches[2] will be an array holding the corresponding names. Quote Link to comment Share on other sites More sharing options...
supermerc Posted July 10, 2009 Author Share Posted July 10, 2009 I see, however I noticed that you put \s* which i think means a white space? However the code i showed you was cleaned up a bit to make it easier for you to understand, the way it appears in the source is like this: <tr> <td style="padding-left:25px;" align="left">Drifters</td> <td><a href="profile.php?id=7929">HardBaLLz</a></td> <td align="center">65</td> </tr> <tr> <td style="padding-left:25px;" align="left">Drifters</td> <td><a href="profile.php?id=22746">0ddBaLL</a></td> <td align="center">66</td> </tr> Does that make a difference? Quote Link to comment Share on other sites More sharing options...
thebadbad Posted July 10, 2009 Share Posted July 10, 2009 Why not try it? \s* matches zero or more whitespace characters, so it should work. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted July 10, 2009 Share Posted July 10, 2009 @OP: Hmm.. my question is why the need to match <td style="padding-left:25px;" align="left">Drifters</td> in the first place? Doesn't look like you're making any use of it. Unless I'm missing something here, couldn't the pattern (based off of thebadbad's) simply be: preg_match_all('~<td><a href="profile.php?id=([^"]+)">([^<]*)<~', $str, $matches); ? Or asked another way, is there any instances where for example <td><a href="profile.php?id=7929">HardBaLLz</a></td> is not directly after td style="padding-left:25px;" align="left">Drifters</td> ? Quote Link to comment Share on other sites More sharing options...
.josh Posted July 11, 2009 Share Posted July 11, 2009 I see, however I noticed that you put \s* which i think means a white space? However the code i showed you was cleaned up a bit to make it easier for you to understand, the way it appears in the source is like this: ... Does that make a difference? Yes it makes a difference. "cleaning up" stuff is exactly the opposite of what you should do when posting regex questions. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.