用preg_replace_callback和str_replace替换文本

时间:2015-11-14 18:08:50

标签: php regex

好的,所以我试图在我的网站评论部分中使用bbcode替换真正长的引号,我正在尝试做的是在已经用js和css编码的崩溃中包含长引号。

我的问题是它会做第一个引用,然后任何其他引用消失。我显然错过了一些东西,但这是我第一次使用这样的回调。

现在我的PHP代码就是这样做的:

$body = preg_replace_callback("/\[quote\](.*?)\[\/quote\]/is",
function($matches)
{
    if (strlen($matches[1]) >= '1000')
    {
        $matches[0] = str_replace($matches[0], '<div class="box"><div class="collapse_container"><div class="collapse_header"><span>Long quote, click to expand</span></div><div class="collapse_content">' . $matches[1] . '</div></div></div>', $matches[0]);
        return $matches[0];
    }
}, $body);

一些示例文字:

[quote]aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa[/quote]

[quote]booohoo[/quote]

[quote]new quoting[/quote]

[b]test[/b]

2 个答案:

答案 0 :(得分:3)

您需要将返回float numTimer = 0.20f; - (void)buttonPressed { _timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:@selector(generateNumber) userInfo:nil repeats:YES]; } - (void)buttonReleased { if ([_timer isValid]) { [_timer invalidate]; } _timer = nil; numTimer = numTimer + 0.1f; _timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:@selector(reGenerateNumber) userInfo:nil repeats:NO]; } } -(void) reGenerateNumber { [self generateNumber]; if ([_timer isValid]) { [_timer invalidate]; } _timer = nil; numTimer = numTimer + 0.1f; _timer = [NSTimer scheduledTimerWithTimeInterval:numTimer target:self selector:@selector(reGenerateNumber) userInfo:nil repeats:NO]; } 代码移到var validPass= await userManager.PasswordValidator.ValidateAsync(txtPassword1.Text); if(validPass.Succeeded) { var user = userManager.FindByName(currentUser.LoginName); user.PasswordHash = userManager.PasswordHasher.HashPassword(txtPassword1.Text); var res= userManager.Update(user); if(res.Succeeded) { // change password has been succeeded } } 块之外:

$matches[0]

另外,我建议按如下方式展开你的懒惰匹配正则表达式:

if

请参阅my regex demo(30个步骤)和your regex demo(2025个步骤)。

请参阅IDEONE demo

答案 1 :(得分:0)

Phps preg *函数默认是贪婪的。它们将匹配正则表达式描述的最长字符串。在你的情况下,正则表达式会计算从第一个[quote]到最后一个[/ quote]的所有内容。要转换此行为,您必须使用“U”修饰符:

$body = preg_replace_callback("/\[quote\](.*?)\[\/quote\]/isU",...);

有关修饰符列表,请参阅http://php.net/manual/en/reference.pcre.pattern.modifiers.php