快速前缀匹配

时间:2012-02-20 22:46:49

标签: php

我需要为字符串列表匹配一定数量的前缀,并且只收集具有这些前缀之一的字符串,如下所示:

$prefixes = array('Dr.', 'Prof.', 'Atty.', 'Rev.');

$results = array();

foreach($names as $name){

  $this_prefix = false;

  foreach($prefixes as $prefix){
    if(strpos($name, $prefix) === 0){
      $this_prefix = $prefix;
    }
  }

  if($this_prefix !== false){
    $name = substr($name, strlen($prefix) + 1);
    $results[$this_prefix][] = $name;
  }

}

上面的代码完成了这项工作,但有没有更快的解决方案?

2 个答案:

答案 0 :(得分:3)

为此类比较而设计的函数是strncmp(),它在O(n)时间内工作(其中n是前缀的长度)。例如:

$prefixes = array('Dr.', 'Prof.', 'Atty.', 'Rev.');

$results = array();

foreach($names as $name){

  $this_prefix = false;

  foreach($prefixes as $prefix){
    if(strncmp($name, $prefix, strlen( $prefix)) == 0){
      $this_prefix = $prefix;
      break;
    }
  }

  if($this_prefix === false){
    continue;
  }

  $name = substr($name, strlen($prefix) + 1);
  $results[$this_prefix][] = $name;

}

并添加break语句,以便在首次匹配后停止。如果你不需要做任何事情,当你不匹配前缀时,你可以使用continue(根据Kernel Coding Style使用超过3个缩进是搞砸了所以我更喜欢使用继续而不是建立大块在if / else)里面

修改

在做了一点研究之后(感谢lolcat的评论)看起来可能的解决方案如下(按性能排序,最好先排序):

  • strpos()
  • strncmp()
  • substr() == $prefix
  • preg_match()

来源:http://maettig.com/code/php/php-performance-benchmarks.php,部分名称为:检查字符串是否以另一个字符串开头。

答案 1 :(得分:2)

尝试正则表达式匹配,那些使用预编译的preg_match函数非常快,表达式可能如下:  /(Dr\\.|Prof\\.|Atty\\.|Rev\\.)/

通过preg_match运行它,你的结果将在matches数组中。我想那就是你要找的东西?