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