开发者

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, $usr开发者_C百科name, 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.


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


\d is what you need instead of /d


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜