Results 1 to 3 of 3

Thread: Regular Expression help

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    4

    Regular Expression help

    Hi guys, I'm having a little trouble, I've tried a few things but I can't get what I want.

    I have the following data:

    Code:
    <a href="/profile.php?id=34214" class="slink">User Abcd</a>
    <a href="/profile.php?id=34243" class="slink">User Abcdef</a>
    <a href="/profile.php?id=34213" class="slink">User Abcdefgh</a>
    <a href="/profile.php?id=34214" class="slink">User Abcdefghij&nbsp;<small><sup>*</sup></small></a>
    <a href="/profile.php?id=34211" class="slink">User Abcdefghijklm</a>
    As you can see, One of the names of the links are different. I just want to match the name and the user ID.

    I have tried the following:

    PHP Code:
    preg_match_all("<a href=\"\/profile.php\?id=(\d+)\" class=\"slink\">(.*?)<\/a>"...... 
    It works fine, Appart from it also returns the &nbsp;<small> etc... (Which is what it's supposed to return)

    But, I don't want that, How do I just not include it? As you see, it's not always there.

    The following works, but not as expected:

    PHP Code:
    preg_match_all("<a href=\"\/profile.php\?id=(\d+)\" class=\"slink\">((\w+)&nbsp;.*?|(\w+))<\/a>"...... 
    Code:
    Array
    (
        [0] => <a href="/profile.php?id=34213" class="slink">User Abcdefgh</a>
        [1] => 34213
        [2] => User Abcdefgh
        [3] => 
        [4] => User Abcdefgh
    )
    Array
    (
        [0] => <a href="/profile.php?id=34214" class="slink">User Abcdefghij&nbsp;<small><sup>*</sup></small></a>
        [1] => 34214
        [2] => User Abcdefghij&nbsp;<small><sup>*</sup></small>
        [3] => User Abcdefghij
    )
    Hope you understand what i'm trying to get to.

    Thanks.

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Regular Expression help

    PHP Code:
    preg_match_all('#<a href="/profile.php\?id=(\d+)" class="slink">([^<&]+)</a>#si', ...); 
    That do what you want?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2008
    Posts
    4

    Re: Regular Expression help

    Nearly, Just isn't matching the link when it has the &nbsp;<smal... after

    matches the others tho

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width