Recursive quote on the forum

I wrote a Quote function for my personal forum on a website written in PHP.

The tags indicated in the tags look like [quote=username]message[/quote], so I wrote this 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 works if the quote is one, but then the user quotes a quote, this does not work. Therefore, to apply this behavior, I need a kind of recursive quote.

I tried to search on many topics, but I really don't understand how this works. Any suggestions / tips for this kind of operation would be appreciated! Let me know and thanks!

EDIT

In 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 is repeated only if the number of occurrences is correct. So it should (right?) Prevent the html from breaking or another malicious attack. What do you think?

+5
3

div . , . :

<?php
function quote($str)
{
    if( preg_match('#\[quote=.*?\](.*)\[/quote\]#i', $str) )
         return quote(preg_replace('#\[quote=.*?\](.*)\[/quote\]#i', '$1', $str);
    return preg_replace('#\[quote=.*?\](.*)\[/quote\]#', '<div blabla>$1</div>', $str);
}
?>
+1

PCRE PHP http://php.net/manual/en/regexp.reference.recursive.php - (?R).

, . , preg_replace_callback .

, ( ) :

= preg_replace_callback('#\[quote=(.*?)\]((?:(?R)|.*?)+)\[/quote\]#is',
          'cb_bbcode_quote', $str);

, $match [1] preg_replace_callback-call.

+4

, . - .

( , ) . , - . .

0

All Articles