[RESOLVED] Help with preg_match regex-VBForums
Results 1 to 5 of 5

Thread: [RESOLVED] Help with preg_match regex

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Posts
    67

    Resolved [RESOLVED] Help with preg_match regex

    Code:
    $regex = '#[0-9]{1,9}(?=">' . $name . '</a>)#';
    preg_match($regex, $html, $matches);
    echo $matches[0];
    The variable $name is in my test case:
    Code:
    house
    And the $html contains this string:
    Code:
    <a href="addonentry.php?fsid=54334">house</a>
    My goal is to get the id you can see there, but it just won't work.
    lol I am used to C#, the regex there makes sense compared to this :P
    Last edited by toxicious; Apr 17th, 2012 at 03:21 PM.

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,504

    Re: Help with preg_match regex

    Seems to be working for me:
    PHP Code:
    $name 'house';
    $html '<a href="addonentry.php?fsid=54334">house</a>';

    $regex '#[0-9]{1,9}(?=">' $name '</a>)#';
    preg_match($regex$html$matches);
    echo 
    $matches[0];

    //echos 54334 
    Is there more to your code?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Posts
    67

    Re: Help with preg_match regex

    Yes there is, sorry for not posting it.
    The $html variable, as you may have guessed, contains the html of a whole page, fetched using file_get_contents.

    It seems that if I put the real html in the $html variable, the thing breaks.

    PHP Code:
    $name "ttt_giant_daycare_V2.zip";
    $url "http://freesteam.org/gmod/search.php?searchterm=" $name;
    $html file_get_contents($url);

    $regex '#[0-9]{1,9}(?=">' $name '</a>)#';
    preg_match($regex$html$matches);
    echo 
    $matches[0]; 
    That is the whole code, my fault for not posting it.
    It can also be executed here, at my dev server:
    http://fredrikblomqvist.developer.se/dev/getaddon.php

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,504

    Re: Help with preg_match regex

    If you view the source of the page you're retrieving, you can see the problem; here's the relevant piece:
    Code:
    <a href='addonentry.php?fsid=38635' >ttt_giant_daycare_V2.zip</a>
    Your pattern is looking for the href to be enclosed with double quotes; this markup's using single quotes. There's also a space between the enclosed href and the tag delimiter. If you want to match this specific case, you could use this pattern instead:
    Code:
    $regex = "#[0-9]{1,9}(?=' >" . $name . "</a>)#";
    Or if you wanted to keep it flexible, something like this also works:
    Code:
    $regex = "#[0-9]{1,9}(?=(\"|')(\s+)?>" . $name . "</a>)#";

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2012
    Posts
    67

    Re: Help with preg_match regex

    god damn it, must have overseen that.
    Thanks a lot for finding the problem and giving me a tip on the regex
    Do you happen to know any good regex builder site for preg_match?
    I have this wonderful thing for C# regexes:
    http://gskinner.com/RegExr/

    The best with that one is that it has a list with all the variables and so on to the right where you fast and easy can check how to do stuff

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.