2017-03-16 52 views
0

这个问题后: Pattern for check single occurrency into preg_match_allstr_pos和preg_match之间哪个更有效?

我明白我的模式必须只包含每个周期一个字,因为,在这个问题报道的情况下,我必须找到“Microsoft”和的“Microsoft Exchange”,我不能修改我的正则表达式,因为这两种可能性是从数据库中给出的!

所以我的问题是:哪个是超过200 preg_match和相同数量的str_pos之间的更好的解决方案,以检查char的子集是否包含这些单词?

我试图写这两个解决方案可能的代码:

$array= array(200+ values); 
foreach ($array as $word) 
{ 
    $pattern='<\b(?:'.$word.')\b>i'; 
    preg_match_all($pattern, $text, $matches); 
    $fields['skill'][] = $matches[0][0]; 
} 

另一种方法是:根据

$array= array(200+ values); 
foreach ($array as $word) 
{ 
    if(str_pos($word, $text)>-1) 
    { 
    fields['skill'][] = $word; 
    } 
} 
+0

基于REGEX的函数比大多数其他字符串函数更慢。顺便说一下,如果你像'$ pattern ='<\ b(?:'。$ word1。'|'。$ word2。'|'。$ word3。'|''那样做你的测试也可以用一个正则表达式来完成。 。$ word4。')\ b> i';'一次可以使用多少个单词取决于正则表达式可以使用多长时间。我创建了12004个字符长的测试正则表达式。似乎不是最大的。 – JustOnUnderMillions

+0

'str_pos()'通常比preg_match快3-20倍,因为preg_match主要用于探测字符串的格式,并根据正则表达式检索它的部分。 –

回答

1

strpospreg_match得多快,这里是一个风向标:

$array = array(); 
for($i=0; $i<1000; $i++) $array[] = $i; 
$nbloop = 10000; 
$text = <<<EOD 
I understand that my pattern must contain only a word per cycle because, in the case reported in that question, I must find "microsoft" and "microsoft exchange" and I can't modify my regexp because these two possibilities are given dinamically from a database! 

So my question is: which is the better solution between over 200 preg_match and the same numbers of str_pos to check if a subset of char contains these words? 
EOD; 

$start = microtime(true); 
for ($i=0; $i<$nbloop; $i++) { 
    foreach ($array as $word) { 
     $pattern='<\b(?:'.$word.')\b>i'; 
     if (preg_match_all($pattern, $text, $matches)) { 
      $fields['skill'][] = $matches[0][0]; 
     } 
    } 
} 
echo "Elapse regex: ", microtime(true)-$start,"\n"; 


$start = microtime(true); 
for ($i=0; $i<$nbloop; $i++) { 
    foreach ($array as $word) { 
     if(strpos($word, $text)>-1) { 
      $fields['skill'][] = $word; 
     } 
    } 
} 
echo "Elapse strpos: ", microtime(true)-$start,"\n"; 

输出:

Elapse regex: 7.9924139976501 
Elapse strpos: 0.62015008926392 

这是快约13倍。

+0

非常感谢你的回答! – Filippo1980

1

正则表达式的功能slowers比大多数其他字符串函数。

通过测试也能做到这一点与一个正则表达式,如果你不喜欢它$pattern='<\b(?:'.$word1.'|'.$word2.'|'.$word3.'|'.$word4.')‌​\b>i';和多少的话,你可以一次使用依赖于正则表达式可以持续多久的方式。我创建了12004个字符长的测试正则表达式。似乎不是最大的。

正则表达式版本(单电):

$array= array(200+ values); 

$pattern='<\b(?:'.implode('|',$array).')\b>i'; 
preg_match_all($pattern, $text, $matches); 
//$fields['skill'][] = $matches[0][0]; 

strpos版本(多呼叫)

$array= array(200+ values); 
foreach ($array as $word){ 
if(strpos($word, $text)!==false)//not with >-1 wont work. 
{ 
    fields['skill'][] = $word; 
} 
} 

如果你在寻找简单的词,strpos将匹配HelloWorldHello, 所以如果你只想要真正的诠释词,你可以这样做:

$arrayOfWords = explode(' ',$string); 
//and now you can check array aginst array 
$array= array(200+ values); 
foreach ($array as $word){ 
if(in_array($word,$arrayOfWords))//not with >-1 wont work. 
{ 
    fields['skill'][] = $word; 
} 
} 
//you can makes this also faster if you array_flip the arrayOfWords 
//and then check with 'isset' (more faster than 'in_array') 

如果您的单词列表中没有这种组合,那么您也希望匹配单词组合(“microsoft exchange”)无法以此方式完成。

*添加评论

+0

谢谢你的回答,但你的正则表达式有问题...正如我所说,如果我在同一短语中寻找“Microsoft”和“microsoft exchange”,您的解决方案将只能找到一个结果! – Filippo1980

+0

@ Filippo1980确定,但只有当您单独查找“microsoft exchange”而不是“microsoft”时,检查的答案才会得到“microsoft exchange”,我的回答更多地指向_what is faster_。而_my regexp_与_your regexp_相同,只是一次寻找多个单词;-)而你的问题实际上关乎性能而不是你想要的结果。 - > _所以我的问题是:..._ – JustOnUnderMillions

相关问题