php 哪种方法是首选的 strstr 或 strpos?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5820586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Which method is preferred strstr or strpos?
提问by danidacar
I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ?
我注意到很多开发人员同时使用 strstr 和 strpos 来检查子字符串是否存在。其中之一是首选,为什么?
回答by Alnitak
From the PHP online manual:
来自 PHP在线手册:
If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function
strpos()
instead.
如果您只想确定特定的针是否出现在 haystack 中,请改用速度更快且内存占用更少的函数
strpos()
。
回答by Sk8erPeter
Hereare some other answers (+benchmarks) I got to my question, which is almostthe same (I didn't realize yours when asking).
这是我对我的问题的一些其他答案(+基准),这几乎是相同的(我在提问时没有意识到你的)。
In the meantime I also made my own benchmark test, which I ran 1000000 times for each relevant functions (strstr()
, strpos()
, stristr()
and stripos()
).
Here's the code:
在此期间,我还做了我自己的基准测试,我跑了100万倍每个相关的功能(strstr()
,strpos()
,stristr()
和stripos()
)。
这是代码:
<?php
function getmicrotime() {
list($usec, $sec) = explode(" ", microtime());
return ((float) $usec + (float) $sec);
}
$mystring = 'blahblahblah';
$findme = 'bla';
echo 'strstr & strpos TEST:<pre>';
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) strstr($mystring, $findme);
$time_needed_strstr = getmicrotime() - $time_start;
echo 'strstr(): ',
round( $time_needed_strstr , 8 ). PHP_EOL;
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) stristr($mystring, $findme);
$time_needed_stristr = getmicrotime() - $time_start;
echo 'stristr(): ',
round( $time_needed_stristr , 8 ) . PHP_EOL;
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) strpos($mystring, $findme) !== false;
$time_needed_strpos = getmicrotime() - $time_start;
echo 'strpos() !== false: ',
round( $time_needed_strpos , 8 ) . PHP_EOL;
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) stripos($mystring, $findme) !== false;
$time_needed_stripos = getmicrotime() - $time_start;
echo 'stripos() !== false: ',
round( $time_needed_stripos , 8 ) . PHP_EOL;
echo PHP_EOL;
echo 'time_needed_stristr - time_needed_strstr: ',
round( $time_needed_stristr - $time_needed_strstr , 8) . PHP_EOL;
echo 'time_needed_stripos - time_needed_strpos: ',
round( $time_needed_stripos - $time_needed_strpos , 8) . PHP_EOL;
echo PHP_EOL;
echo 'time_needed_strstr - time_needed_strpos: ',
round( $time_needed_strstr - $time_needed_strpos , 8) . PHP_EOL;
echo 'time_needed_stristr - time_needed_stripos: ',
round( $time_needed_stristr - $time_needed_stripos , 8) . PHP_EOL;
echo '</pre>';
?>
And here is the first output, which shows that strpos()
is the winner:
这是第一个输出,表明它strpos()
是赢家:
strstr & strpos TEST:
strstr(): 2.39144707
stristr(): 3.65685797
strpos() !== false: 2.39055395
stripos() !== false: 3.54681897
time_needed_stristr - time_needed_strstr: 1.2654109
time_needed_stripos - time_needed_strpos: 1.15626502
time_needed_strstr - time_needed_strpos: 0.00089312
time_needed_stristr - time_needed_stripos: 0.110039
The next one is similar to the first output (strpos()
is the winner again):
下一个类似于第一个输出(又strpos()
是赢家):
strstr & strpos TEST:
strstr(): 2.39969015
stristr(): 3.60772395
strpos() !== false: 2.38610101
stripos() !== false: 3.34951186
time_needed_stristr - time_needed_strstr: 1.2080338
time_needed_stripos - time_needed_strpos: 0.96341085
time_needed_strstr - time_needed_strpos: 0.01358914
time_needed_stristr - time_needed_stripos: 0.25821209
Below is another one, which is more interesting, because in this case, strstr()
is the winner:
下面是另一个更有趣的,因为在这种情况下,strstr()
是赢家:
strstr & strpos TEST:
strstr(): 2.35499191
stristr(): 3.60589004
strpos() !== false: 2.37646604
stripos() !== false: 3.51773095
time_needed_stristr - time_needed_strstr: 1.25089812
time_needed_stripos - time_needed_strpos: 1.14126492
time_needed_strstr - time_needed_strpos: -0.02147412
time_needed_stristr - time_needed_stripos: 0.08815908
This means it can really depend on "environmental circumstances", which are sometimes hard to influence, and can change the result of "micro optimization tasks" like this, in case you are just checking whether a string exists in another one or not.
这意味着它真的可以依赖于有时难以影响的“环境情况”,并且可以像这样改变“微优化任务”的结果,以防您只是检查一个字符串是否存在于另一个字符串中。
BUT I think in most cases, strpos()
is the winnerin comparison to strstr()
.
但我认为在大多数情况下,strpos()
是赢家相比strstr()
。
I hope this test was useful for someone.
我希望这个测试对某人有用。
回答by mario
Many developers use strpos
for micro optimizationpurposes.
许多开发人员strpos
用于微优化目的。
Using strstr
also only works if the resulting string cannot be interpreted as false in boolean context.
使用strstr
也只能如果生成的字符串不能被解释为布尔上下文假。
回答by Flask
strpos() detects where in the haystack a particular needle lies. stristr() tests whether the needle is anywhere in the haystack
strpos() 检测特定针在大海捞针中的位置。stristr() 测试针是否在大海捞针的任何地方
therefor strpos() is faster and less memory consuming
因此 strpos() 速度更快,内存消耗更少
a reason for strstr(): if your needle is at the beginning of a string, strpos returns 0 (so have to check it with === false)
strstr() 的一个原因:如果您的指针位于字符串的开头,则 strpos 返回 0(因此必须使用 === false 进行检查)
回答by T.Todua
I prefer strstr()
for readability and easy coding.. strpos() !==false
is confusing a bit..
我更喜欢strstr()
可读性和简单的编码.. strpos() !==false
有点混乱..