我正在尝试从我的公告板中删除嵌套引用,但我遇到了一些问题。
示例输入:
[引用作者=personX 链接=topic=12.msg1910#msg1910 date=1282745641]
[quote author=PersonY link=topic=12.msg1795#msg1795 date=1282727068] The message in the original quote [/quote]
第二条消息引用了第一条消息
[/引用]
[引用作者=PersonZ 链接=主题=1.msg1#msg1 日期=1282533805]
随机的第三个报价
[/引用]
示例输出
[引用作者=personX 链接=topic=12.msg1910#msg1910 date=1282745641]
第二个报价中的消息
[/引用]
[引用作者=PersonZ 链接=主题=1.msg1#msg1 日期=1282533805]
随机的第三个报价
[/引用]
如您所见,嵌套的引号(原始消息)与引号标记一起被删除。
我似乎无法弄清楚。
当我尝试
$toRemove = '(\\[)(quote)(.*?)(\\])';
$string = $txt;
$found = 0; echo preg_replace("/($toRemove)/e", '$found++ ? \'\' : \'$1\'', $string);
它会删除除第一个之外的所有引用标签,
但是当我将代码扩展为:
$toRemove = '(\\[)(quote)(.*?)(\\])(.*?)(\\[\\/quote\\])';
$string = $txt;
$found = 0; echo preg_replace("/($toRemove)/e", '$found++ ? \'\' : \'$1\'', $string);
它根本停止做任何事情。
对此有什么想法吗?
编辑:
谢谢你的帮助,哈吉。
不过,我总是遇到麻烦。
while 循环
while ( $input = preg_replace_callback( '~\[quoute.*?\[/quote\]~i', 'replace_callback', $input ) ) {
// replace every occurence
}
导致页面无限循环,当被删除时(连同额外的 u 在 quoute 中),页面不做任何事情。
我已经确定原因是匹配
当更改为
$input = preg_replace_callback( '/\[quote(.*?)/i', 'replace_callback', $input );
代码确实开始工作,但是当更改为
$input = preg_replace_callback( '/\[quote(.*?)\[\/quote\]/i', 'replace_callback', $input );
它再次停止做任何事情。
此外,undo_replace 函数存在一个问题,因为它永远不会找到存储的哈希值,它只会给出有关未找到索引的警告。我猜与 sha1 匹配的正则表达式无法正常工作。
我现在拥有的完整代码:
$cache = array();
$input = $txt;
function replace_callback( $matches ) {
global $cache;
$hash = sha1( $matches[0] );
$cache["hash"] = $matches[0];
return "REPLACE:$hash";
}
// replace all quotes with placeholders
$input = preg_replace_callback( '/\[quote(.*?)\[quote\]/i', 'replace_callback', $input );
function undo_replace( $matches ) {
global $cache;
return $cache[$matches[1]];
}
// restore the outer most quotes
$input = preg_replace_callback( '~REPLACE:[a-f0-9]{40}~i', 'undo_replace', $input );
// remove the references to the inner quotes
$input = preg_replace( '~REPLACE:[a-f0-9]{40}~i', '', $input );
echo $input;
再次感谢任何想法家伙:)