解析 BBCode 并从 BB 标签中提取特定属性
我需要从我的论坛(PHP)的引号中获取用户名。
我正在搜索的内容将如下所示:
[quote author=username link=topic=1234.msg1234567#1234567 date=1234567890]
lorem ipsum dolor
[/quote]
lorem ipsum dolor sit amet
我需要的只是 username
属性值。
最大的问题是一篇文章可能有多个引号,因此有多个用户名,所以我需要将每个名称放入一个数组中。
I need to get the username out of quotes for my forum (PHP).
The content I'm searching will be like this:
[quote author=username link=topic=1234.msg1234567#1234567 date=1234567890]
lorem ipsum dolor
[/quote]
lorem ipsum dolor sit amet
All I need is the username
attribute value.
The big problem is a post could have multiple quotes and therefore multiple usernames, so I need to get each name into an array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 preg_match_all() - http://php.net/manual/en/ function.preg-match-all.php ,您将得到匹配的结果
preg_match_all('/author=(\w+)/i', $string, $usernames);
编辑:
\w - 任何“单词”字符。 “单词”字符是任何字母、数字或下划线字符,即可以是 Perl“单词”一部分的任何字符。如果用户名仅包含字母,您可以使用 [az] 进行更改。
尝试一下 preg_match_all('/author=(.+)\s+link/i', $string, $usernames);
\s - 任何空白字符
Use preg_match_all() - http://php.net/manual/en/function.preg-match-all.php and you will have the result in matches
preg_match_all('/author=(\w+)/i', $string, $usernames);
Edit:
\w - any "word" character. A "word" character is any letter or digit or the underscore character, that is, any character which can be part of a Perl "word". You coul dchange that with [a-z] if the username contains only letters.
Try it like that preg_match_all('/author=(.+)\s+link/i', $string, $usernames);
\s - any whitespace character
我无法详细帮助您使用 php,但正则表达式应该如下所示:“quoteauthor=([A-Za-z]*)”
然后您访问组集合以获取名称。 “([A-Za-z]*)”定义您要访问的组。
I can't help you with php in detail, but the Regex should look like this: "quote author=([A-Za-z]*)"
Then you access the groups collection to get the name. "([A-Za-z]*)" defines the group you want to access.
另外,如果你想在 RegEx 方面做得更好 - 和他们一起玩。
试用 RegExhibit (Mac) http://homepage.mac.com/roger_jolly/software/或正则表达式教练 (Win) http://www.weitz.de/regex-coach/
两者都是免费的并且非常有用。
Also, if you want to get better at RegEx - play with them.
Try out RegExhibit (Mac) http://homepage.mac.com/roger_jolly/software/ or Regex Coach (Win) http://www.weitz.de/regex-coach/
Both are free and really useful.