Jump to content

Preg_match_all


supermerc

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/165505-preg_match_all/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/165505-preg_match_all/#findComment-872916
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/165505-preg_match_all/#findComment-872920
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/165505-preg_match_all/#findComment-873237
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/165505-preg_match_all/#findComment-873264
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.