tags:

views:

56

answers:

4

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.

A: 

Try to use PEAR BBCode Parser

Alexander.Plutov
A: 

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
the `author=` part may not always be at the start of the `quote` tag
ar
then it could work with "quote.*?author=([A-Za-z]*).*?quote". But I honestly don't know and haven't tried it.
testalino
+1  A: 

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
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
+1  A: 

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