0

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.

Matt
  • 1,083
  • 2
  • 10
  • 15

4 Answers4

2

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
  • 10,555
  • 1
  • 31
  • 31
  • 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 Jan 01 '10 at 20:57
  • You'll also need to escape the forward slash in the /a : \/a Note other poster's comments about allowing more than one digit and escaping the ? symbol – mopoke Jan 01 '10 at 21:03
0

\d is what you need instead of /d

Valentin Golev
  • 9,965
  • 10
  • 60
  • 84
0

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

Paul Creasey
  • 28,321
  • 10
  • 54
  • 90
0

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
  • 328,213
  • 58
  • 503
  • 561