preg_match()与strpos()进行匹配查找? [英] preg_match() vs strpos() for match finding?
本文介绍了preg_match()与strpos()进行匹配查找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于单值检查,两者中的哪一个是首选,为什么?
For single value check, which of both is preferred and why?
$string == 'The quick brown fox jumps over the lazy dog';
if(strpos($string, 'fox') !== false){
// do the routine
}
# versus
if(preg_match('/fox/i', $string)){
// do the routine
}
推荐答案
相对于preg_match
,我更喜欢strpos
,因为执行正则表达式通常更昂贵.
I would prefer the strpos
over preg_match
, because regexes are generally more expensive to execute.
根据 preg_match
的官方php文档:
According to the official php docs for preg_match
:
如果仅使用
preg_match()
,请不要使用 想检查一个字符串是否 包含在另一个字符串中.使用strpos()
或strstr()
代替 会更快.
Do not use
preg_match()
if you only want to check if one string is contained in another string. Usestrpos()
orstrstr()
instead as they will be faster.
这篇关于preg_match()与strpos()进行匹配查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文