Мне нужно заменить цитаты BBCode с форума phpBB3 с помощью PHP. Цитаты постов выглядят так:
[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 ) )
Это то, что мне нужно, но я не понимаю цитируемый текст. Только имена пользователей. Любая помощь? Спасибо.