I need to replace BBCode quotes from a phpBB3 forum using PHP. Quoted posts look like this:
[quote="John Doe":2sxn61wz][quote="Bob":2sxn61wz]Some text from Bob[/quote:2sxn61wz]Some text from John Doe[/quote:2sxn61wz]Some more text
I would like to parse this string and end up with an array like:
Array (
[0] => Array (
[0] => 'John Doe'
[1] => 'Some text from John Doe'
)
[1] => Array (
[0] => 'Bob'
[1] => 'Some text from Bob'
)
)
What would be the best approach to recursively find these quote blocks and their content? Thanks in advance for any help on that!
As suggested in the comments:
$str = '[quote="John Doe":2sxn61wz][quote="Bob":2sxn61wz]Some text from Bob[/quote:2sxn61wz]Some text from John Doe[/quote:2sxn61wz]Some more text';
$uid = '2sxn61wz';
print_r(quoteParser($str, $uid));
function quoteParser($str, $uid) {
$pattern = "#\[quote(?:="(.*?)")?:$uid\]((?!\[quote(?:=".*?")?:$uid\]).)?#ise";
echo "Unparsed string: " . $str . "<br /><br />";
echo "Pattern: " . $pattern . "<br /><br />";
preg_match_all($pattern, $str, $matches);
return $matches;
}
Output:
Array ( [0] => Array ( [0] => [quote="John Doe":2sxn61wz] [1] => [quote="Bob":2sxn61wz]S ) [1] => Array ( [0] => John Doe [1] => Bob ) [2] => Array ( [0] => [1] => S ) )
That’s quite what I need but I don’t get the quoted text. Only the user names. Any help? Thanks so far.
Apparently what you’re wanting is to turn your BBCode output into HTML. You can check the
bbcode_create()function if you like. Otherwise a different recursion -to format your text- is a better option, I believe.You can call this by
echo str2html('some string here'). You get the idea.