2015-10-15 55 views
0

有没有办法在达到某个索引时中断搜索preg_replace_callbackPHP preg_replace_callback有没有办法打破搜索?

例如:

  $content = preg_replace_callback('/'.preg_quote($_POST['query'],'#').'/i', function($matches) use($replacements,&$index) { 
       if ($index > count($replacements)) { 
        // I tried break; but doesn't work 
       } 

       if ($replacements[$index] != '') { 
        $matches[0] = $replacements[$index]; 
       } 
       $index++; 
       return $matches[0]; 
      }, $content); 
+0

@Kasramvd我已经在上面添加了一个示例代码。 – JohnnyQ

回答

1

您可以只使用ELSEIF忽略第二,如果

 $content = preg_replace_callback('/'.preg_quote($_POST['query'],'#').'/i', function($matches) use($replacements,&$index) { 
      if ($index > count($replacements)) { 
       // I tried break; but doesn't work 
      } elseif ($replacements[$index] != '') { 
       $matches[0] = $replacements[$index]; 
      } 
      $index++; 
      return $matches[0]; 
     }, $content); 

编辑

preg_replace_callback (mixed pattern, callback callback, mixed subject [, int limit [, int &count]]) 

限制 最大为每个模式中可能的替代品每个主题字符串。默认为-1(没有限制)。

count 如果指定,则此变量将填充完成的替换次数。

+0

有没有办法停止搜索? – JohnnyQ

+1

Actualy你可以限制全部替换它唯一的另一种方法是编写自己的函数来遍历数组。 –

相关问题