-
Apr 17th, 2012, 02:04 PM
#1
Thread Starter
Lively Member
[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:
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 02:21 PM.
-
Apr 17th, 2012, 04:41 PM
#2
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?
-
Apr 18th, 2012, 05:01 AM
#3
Thread Starter
Lively Member
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
-
Apr 18th, 2012, 11:59 AM
#4
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>)#";
-
Apr 18th, 2012, 12:26 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|