views:

46

answers:

4

On a phpBB forum, memberlist.php lists all the members of the board with the following HTML:

<a href="profile.php?mode=viewprofile&amp;u=4">Username</a>

Where u=4 is the UserID of the user, and Username is obviously their username.

There are probably 50-100 peices of HTML like this and I would like to match them all, so I was going to use preg_match_all.

This is what I got:

preg_match_all('/<a href="profile\.php?mode=viewprofile&amp;u=/d">(.*?)</a>/', $page, $usrname, PREG_PATTERN_ORDER);

But it returns this error:

Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'd' in C:\xampp\htdocs\index.php on line 38

Could anyone tell me the regex to use in the preg_match_all function to match the usernames? Bearing in mind the u=4 part of the link will change :)

Cheers.

+2  A: 

You should use \d instead of /d

/d is an attempt to use a modifier (such as /i for case insensitivity)

\d is a character class meaning the numbers 0-9.

This should work:

preg_match_all('/<a href="profile\.php\?mode=viewprofile&amp;u=\d+">(.*?)<\/a>/', $page, $usrname, PREG_PATTERN_ORDER);
mopoke
After I do that I get an `Unknown modifier 'a'` error, and when I do this: `<\/a>` I get an empty two-dimensional array returned.
Matt
You'll also need to escape the forward slash in the /a : \/aNote other poster's comments about allowing more than one digit and escaping the ? symbol
mopoke
Works perfect, thanks.
Matt
A: 

\d is what you need instead of /d

valya
A: 

\d instead of /d, you'll also want to escape the ? at the start of the query string

Paul Creasey
A: 

Use

preg_match_all('%<a href="profile\.php\?mode=viewprofile&amp;u=\d+">(.*?)</a>%', $page, $usrname, PREG_PATTERN_ORDER);

Use \d+ instead of /d (which is a syntax error). The + is to allow more than one digit (I guess you'll have more than 10 users, don't you)? Also escape the ?, or it means "zero or one occurences of the previous character/expression. Since you have a slash in your regex, you can't use it as a delimiter, so I used the percent sign % instead.

Tim Pietzcker
You missed the `/` in the `</a>`. Should be `<\/a>`.
Blair McMillan
Nope. I changed the regex delimiters to `%`, thereby making it unneccessary to escape the `/`.
Tim Pietzcker