[noob] Need help

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
czech_d3v3l0p3r
Forum Newbie
Posts: 10
Joined: Sat Jan 24, 2009 1:57 pm

[noob] Need help

Post by czech_d3v3l0p3r »

Hello everybody.

I'm pretty noob in regexes and I need a help assembling an expression.

So basicly I have something like this HMTL code:

Code: Select all

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#9633; <a target=blank href="/members/[color=#4080FF]1234[/color]">[color=#FF8040]User01[/color]</a> <a href="javascript&#058;;" onclick="ShowDiv('gUser01');"><img src="http://site.com/arrow.gif"></a><span id="gAmen" style="display:none"><ul><img src="http://sizr.com/ico/dline.gif"> <b>User01's group memberships:</b><br><br>
- [color=#408000]Maps[/color]<br>
- [color=#408000]Sprites[/color]<br>
- [color=#408000]Textures[/color]<br>
- [color=#408000]Scripts[/color]<br>
- [color=#408000]Sounds[/color]<br>
- [color=#408000]Posts[/color]<br>
- [color=#408000]IP search[/color]</ul></span><br>
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#9633; <a target=blank href="/members/[color=#4080FF]45678[/color]">[color=#FF8040]User02[/color]</a> <a href="javascript&#058;;"  onclick="ShowDiv('gUser02');"><img src="http://sitep.com/arrow.gif"></a><span id="g[$carface]" style="display:none"><ul><img src="http://site.com/ico/dline.gif"> <b>User02's group memberships:</b><br><br>
- [color=#408000]Assessments[/color]<br>
- [color=#408000]Appeals[/color]<br>
- [color=#408000]Clubs[/color]<br>
- [color=#408000]Forum[/color]<br>
- [color=#408000]Gaghammer[/color]<br>
- [color=#408000]GUIs[/color]<br>
- [color=#408000]IP search[/color]<br>
- [color=#408000]News[/color]<br>
- [color=#408000]Posts[/color]<br>
- [color=#408000]Sprites[/color]<br>
- [color=#408000]Users[/color]</ul></span><br>
 
and I'd like to get the highlighted data: User ID, User Name, User's Group Memberships. BTW I'm using PHP's preg_match_all( $pattern, $subject, &$matches ); function.

Any help would be greatly appreciated.

PS: Sorry for my bad English and not much details provided I'm a bit tired.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: [noob] Need help

Post by prometheuzz »

czech_d3v3l0p3r wrote:...
not much details provided I'm a bit tired.
Well, perhaps you should get a good night's sleep and then post back with enough details and perhaps post what you have tried so far.
I mean, you wouldn't like it if people started answering your question without actually reading your question or providing wrong answers just because they were too tired to post a proper answer, right?
czech_d3v3l0p3r
Forum Newbie
Posts: 10
Joined: Sat Jan 24, 2009 1:57 pm

Re: [noob] Need help

Post by czech_d3v3l0p3r »

prometheuzz wrote:
czech_d3v3l0p3r wrote:...
not much details provided I'm a bit tired.
Well, perhaps you should get a good night's sleep and then post back with enough details and perhaps post what you have tried so far.
I mean, you wouldn't like it if people started answering your question without actually reading your question or providing wrong answers just because they were too tired to post a proper answer, right?
Thanks.

And here's what I tried so far:

Code: Select all

$subject = file_get_contents(URL);
$matches = array();
preg_match_all("@<a .*?href=\"/members/(\d+)\">(.*?)</a>.*?</b><br><br>\n*(- ([a-z]+).*?<br>)+@im", $subject, &$matches);
but it returned nothing.

Thanks in advance for any suggestions.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: [noob] Need help

Post by prometheuzz »

Okay, it's not possible to do this in one go since the "membership" list does not have a fixed size per user. You could do it like this:

Code: Select all

<?php
$text =<<< BLOCK
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#9633; <a target=blank href="/members/1234">User01</a> 
<a href="javascript&#058;;" onclick="ShowDiv('gUser01');"><img src="http://site.com/arrow.gif">
</a><span id="gAmen" style="display:none"><ul><img src="http://sizr.com/ico/dline.gif"> 
<b>User01's group memberships:</b><br><br>
- Maps<br>
- Sprites<br>
- Textures<br>
- Scripts<br>
- Sounds<br>
- Posts<br>
- IP search</ul></span><br>
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#9633; <a target=blank href="/members/45678">User02</a> 
<a href="javascript&#058;;"  onclick="ShowDiv('gUser02');"><img src="http://sitep.com/arrow.gif">
</a><span id="g[\$carface]" style="display:none"><ul><img src="http://site.com/ico/dline.gif"> 
<b>User02's group memberships:</b><br><br>
- Assessments<br>
- Appeals<br>
- Clubs<br>
- Forum<br>
- Gaghammer<br>
- GUIs<br>
- IP search<br>
- News<br>
- Posts<br>
- Sprites<br>
- Users</ul></span><br>
BLOCK;
 
if(preg_match_all('@href="/members/(\d+)"[^>]*>([^<]*).*?<ul>(.*?)</ul>@is', $text, $matches, PREG_SET_ORDER)) {
  foreach($matches as $match) {
    echo "ID={$match[1]}, name={$match[2]}\n";
    if(preg_match_all('/(?<=-\s)[^<]*/', $match[3], $memberships)) {
      print_r($memberships);
    }
  }
}
?>
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: [noob] Need help

Post by pickle »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
czech_d3v3l0p3r
Forum Newbie
Posts: 10
Joined: Sat Jan 24, 2009 1:57 pm

Re: [noob] Need help

Post by czech_d3v3l0p3r »

prometheuzz wrote:Okay, it's not possible to do this in one go since the "membership" list does not have a fixed size per user. You could do it like this:

...code...
You're an awesome guy! Thank you very much, that really helped me. ^_^
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

Re: [noob] Need help

Post by prometheuzz »

czech_d3v3l0p3r wrote:
prometheuzz wrote:Okay, it's not possible to do this in one go since the "membership" list does not have a fixed size per user. You could do it like this:

...code...
You're an awesome guy! Thank you very much, that really helped me. ^_^
; )
Thanks, and you're welcome.
Post Reply