Recursive quote in a forum

I have wrote a Quote function for my own personal forum, in a website written with PHP.

The message quoted tags looks like [quote=username]message[/quote], so I wrote that function :

$str=preg_replace('#\[quote=(.*?)\](.*?)\[/quote\]#is', '<div class="messageQuoted"><i><a href="index.php?explore=userview&userv=$1">$1</a> wrote :</i>$2</div>', $str);

This one works if the quote is one, but then a user quote a quote, this doesnt works. So I need a sort of recursive quote for apply this behaviour.

I tried to searching on SO many topics, but I don't really understand how it can works. Would be appreciated any suggestions/tips for do this kind of operation! Let me know, and thanks!

EDIT

At the end, this is my own solution :

if(preg_match_all('#\[quote=(.*?)\](.*?)#is', $str, $matches)==preg_match_all('#\[/quote\]#is', $str, $matches)) {
    array_push($format_search, '#\[quote=(.*?)\](.*?)#is');
    array_push($format_search, '#\[/quote\]#is');

    array_push($format_replace, '<div class="messageQuoted"><a class="lblackb" href="index.php?explore=userview&userv=$1">$1</a> wrote :<br />$2');
    array_push($format_replace, '</div>');
}

$str=preg_replace($format_search, $format_replace, $str);

it repleace only if the number of occurences is correct. So it should (right?) to prevent html broke or other malicious attack. What do you think?

5
задан kwichz 9 May 2011 в 15:27
поделиться