微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 论坛中的递归引用

我在一个PHP编写的网站上为自己的个人论坛编写了一个引用函数.

引用的标签看起来像[quote = username] message [/ quote],所以我写了这个函数

$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);

如果引用是一个,但是当用户引用引用时,这个不起作用,这不起作用.所以我需要一种递归引用来应用这种行为.

我试图搜索很多主题,但我真的不明白它是如何工作的.
将不胜感激任何有关此类操作的建议/提示!让我知道,谢谢!

编辑

最后,这是我自己的解决方案:

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);

只有在出现次数正确的情况下才能补充.所以它应该(对吧?)来防止html破坏或其他恶意攻击.你怎么看?

解决方法:

您只需将开始引号标记替换为开始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);
}
?>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐