SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Regex - whats wrong with this?

    Hey,

    I've been trying to make this damn regex work for 3.5hours now, and so far - have nothing!

    I need to preg_match a string for this, and get the number, in this case 47, out if it, but the foo variable wont be the same, so should be ignored:

    <span class="cut"> ...&nbsp;</span><a href="blah.php?foo=bar&page=47">

    Code PHP:
    preg_match('|\<span class\=\"do\"> ...&nbsp;<\/span><a href\=\"blah.php?foo=(.)&page=([^\"])"\>|', $contents, $matches);

    Anyone know whats wrong?

    Thanks

    fLUx

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    "...&nbsp" <- a dot is a wildcard in regex, so escape all three of those

    "foo=(.)" <- that dot will only match a single character; use (.*) to match any number of characters

  3. #3
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply!

    Although, either I don't understand what you mean, or something else in the expression isn't right:

    PHP Code:
    preg_match('|\<span class\=\"do\"> \.\.\.\&nbsp;\<\/span><a href\=\"blah.php?foo=(.*)&page=([^\"])"\>|'$contents$matches); 
    Whats wrong with it now?

    Thanks!

    fLUx

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Dunno, I started fresh. This'll capture the foo and page values:

    Code:
    #\<span class=\"cut\"\> \.\.\.&nbsp;\<\/span\>\<a href=\"blah.php\?foo=(.*)&page=([0-9]*)\"\>#
    Gave it a whirl at the regular expression tester to make sure it worked.

  5. #5
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    woo!

    Thanks mate, I will compare it now to see what I did wrong...

    Although, its outputting all the rest of the document after it too:

    PHP Code:
    Array
    (
        [
    0] => <span class="cut"> ...&nbsp; ......etc........
        [
    1] => d&page=47"> 47&nbsp; ......etc........
        [2] => 2

    You know why? Sure its just something simple...

    Thanks!

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Your input must not look like the string you said you wanted to match. The matches for that string are [1]=foo, [2]=47. Add a ^ at the beginning of the pattern and $ at the end and see if that's any better for ya.

  7. #7
    SitePoint Enthusiast fLUx1337's Avatar
    Join Date
    Jul 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help, all working now!


Bookmarks

Posting Permissions

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