我需要使用 PHP 替换 phpBB3 论坛中的 BBCode 引号。引用的帖子如下所示:
[quote="John Doe":2sxn61wz][quote="Bob":2sxn61wz]Some text from Bob[/quote:2sxn61wz]Some text from John Doe[/quote:2sxn61wz]Some more text
我想解析这个字符串并最终得到一个数组,如:
Array (
[0] => Array (
[0] => 'John Doe'
[1] => 'Some text from John Doe'
)
[1] => Array (
[0] => 'Bob'
[1] => 'Some text from Bob'
)
)
递归查找这些引用块及其内容的最佳方法是什么?在此先感谢您的帮助!
正如评论中所建议的:
$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;
}
输出:
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 ) )
这正是我所需要的,但我没有得到引用的文字。只有用户名。有什么帮助吗?感谢到目前为止。