使用正则表达式匹配成员列表中的用户名
在 phpBB 论坛上,memberlist.php 使用以下 HTML 列出了董事会的所有成员:
<a href="profile.php?mode=viewprofile&u=4">Username</a>
其中 u=4 是用户的 UserID,Username 显然是他们的用户名。
可能有 50-100 个这样的 HTML,我想将它们全部匹配,所以我打算使用 preg_match_all
。
这就是我得到的:
preg_match_all('/<a href="profile\.php?mode=viewprofile&u=/d">(.*?)</a>/', $page, $usrname, PREG_PATTERN_ORDER);
但它返回此错误:
Warning: preg_match_all() [function.preg-match-all]: Unknown modifier 'd' in C:\xampp\htdocs\index.php on line 38
有人能告诉我在 preg_match_all
函数中使用的正则表达式来匹配用户名吗?请记住链接的 u=4
部分将会更改:)
干杯。
On a phpBB forum, memberlist.php lists all the members of the board with the following HTML:
<a href="profile.php?mode=viewprofile&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&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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该使用
\d
而不是/d
/d 是尝试使用修饰符(例如 /i 不区分大小写)
\d 是一个字符类,表示数字0-9。
这应该有效:
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:
\d 是你需要的而不是 /d
\d is what you need instead of /d
\d 而不是 /d,您还想转义 ?在查询字符串的开头
\d instead of /d, you'll also want to escape the ? at the start of the query string
使用
Use
\d+
而不是/d
(这是一个语法错误)。+
允许超过一位数字(我猜您将拥有超过 10 个用户,不是吗)?还要转义?
,或者它的意思是“前一个字符/表达式出现零次或一次。由于正则表达式中有斜杠,所以不能将其用作分隔符,因此我使用了改为百分号%
。Use
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.