Search code examples
regexphpbbpreg-match-all

Matching usernames in a memberlist using regex


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.


Solution

  • 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);