1

Possible Duplicate:
Best way to parse bbcode

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'.

The big problem is a post could have multiple quotes and therefore multiple usernames, so I need to get each name into an array, and my regex skills are poor.

Community
  • 1
  • 1
katoth
  • 73
  • 5

3 Answers3

1

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

Yasen Zhelev
  • 4,045
  • 3
  • 31
  • 56
  • Thanks this is /almost/ what I need, however, usernames may contain spaces and special characters, so I actually need to grab everything before "link=". What does \w+ mean? – katoth Oct 19 '10 at 10:33
1

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.

Colin
  • 784
  • 2
  • 8
  • 21
0

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.

testalino
  • 5,474
  • 6
  • 36
  • 48