Reputation: 2203
[quote=Username here]quoted text here[/quote]
Reply text here
I need a regular expression that stores the "Username here", "quoted text here" and "Reply text here" in a Array.
This expression needs to support nesting aswell. Eks:
[quote=Username2 here][quote=Username here]quoted text here[/quote]
Reply text here[/quote]
Reply text here
Upvotes: 0
Views: 102
Reputation: 170148
This regex matches nested quote block (in group 1) with an additional last reply (in group 2):
(\[quote=[^]]*](?:(?R)|.)*\[/quote])(.*)
A little demo:
$text = '[quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]More text';
preg_match('#(\[quote=[^]]*](?:(?R)|.)*\[/quote])(.*)#is', $text, $match);
print_r($match);
produces:
Array
(
[0] => [quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]More text
[1] => [quote=Username2 here][quote=Username here]quoted text[/quote]Reply text[/quote]
[2] => More text
)
A little explanation:
( # open group 1
\[quote=[^]]*] # match '[quote= ... ]'
(?:(?R)|.)* # recursively match the entire pattern or any character and repeat it zero or more times
\[/quote] # match '[/quote]'
) # open group 1
( # open group 2
.* # match zero or more trailing chars after thae last '[/quote]'
) # close group 2
But, using these recursive regex constructs supported by PHP might make ones head spin... I'd opt for a little parser like John Kugelman suggested.
Upvotes: 3
Reputation: 2553
Assuming you do not want to return the values nested in some way or with quotes matched - which are impossible in a regex - you can just split on the parts you do not need:
preg_split('/(\[quote=|\[quote]|]|\[/quote])/', $yourstring);
Upvotes: 0