【问题标题】:preg_replace search for string that does not match the pattern instead of string that match the patternpreg_replace 搜索不匹配模式的字符串而不是匹配模式的字符串
【发布时间】:2013-07-11 15:59:24
【问题描述】:

我想用preg_replace 函数替换这个可能值数组:

$attr = array('b','i','u','left','center','right');

foreach($attr as $a) {
    // strip bbcode
    $str = preg_replace('#\['.$a.'\](.*)\[/'.$a.'\]#im','$1',$str); 
}
return $str;

但我不想使用 foreach 并循环遍历每个可能的排除项,而是想像剥离任何不是 img 的东西一样。这可能吗?

【问题讨论】:

    标签: php regex preg-replace bbcode


    【解决方案1】:

    您的意思是要删除除 img 标记之外的所有标记?使用这个:

    $str = preg_replace('#\[(?!/?img).+?]#im','',$str);
    

    【讨论】:

    • 您的模式对我有用,尽管您的代码中有错误。说'preg_replace()[function.preg-replace]:分隔符不能是字母数字或反斜杠',如果通过添加分隔符来修复它。谢谢顺便说一句
    • 哦,很抱歉...我在 PowerShell 提示符下测试了正则表达式(PS 中没有正则表达式分隔符,只有引号)并从那里复制。我将添加分隔符和模式修饰符以修复后代的答案。
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2011-09-08
    相关资源
    最近更新 更多