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?