哪种方法首选strstr或strpos?

dan*_*car 77 php strstr strpos

我注意到很多开发人员正在使用strstr和strpos来检查子字符串是否存在.他们中的一个是首选,为什么?

Aln*_*tak 118

从PHP 在线手册:

如果您只想确定特定针是否出现在haystack中,请使用更快且内存更少的内存密集型函数strpos().

  • +1,你可以使用strpos或stripos.并且不要忘记检查关于使用=== FALSE的php doc上的警告; (13认同)
  • 详细说明fedmich的评论:我总是使用`if(strpos($ haystack,$ needle)!== false){//做某事}`,永远不要`if(strpos($ haystack,$ needle)){// do坏事}`.如果`$ needle`位于`$ haystack`的最开头,`strpos`将返回0,并且0被认为等于false.`(0 == false)`计算结果为true.`(0 === false)`计算结果为假. (7认同)

Sk8*_*ter 34

这里有一些其他的答案(+基准)我得到了我的问题,这几乎是一样的(我在询问时没有意识到你的问题).


在此期间,我还做了我自己的基准测试,我跑了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>';

?>
Run Code Online (Sandbox Code Playgroud)

这是第一个输出,显示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 
Run Code Online (Sandbox Code Playgroud)

下一个类似于第一个输出(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
Run Code Online (Sandbox Code Playgroud)

下面是另一个更有趣的,因为在这种情况下,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
Run Code Online (Sandbox Code Playgroud)

这意味着它可以真正依赖于"环境条件",这有时很难影响,并且可以改变这样的"微优化任务"的结果,以防您只是检查字符串是否存在于另一个字符串中.

但我认为在大多数情况下,strpos()与之相比是胜利者strstr().

我希望这个测试对某人有用.

  • 虽然这个基准测试很有用,但它不能测量内存消耗,也没有考虑长字符串,如kbytes或mbytes. (3认同)

mar*_*rio 6

许多开发人员strpos用于微优化目的.

使用strstr也仅在结果字符串在布尔上下文中不能解释为false时才有效.

  • 它不是微优化,它被称为_使用job_的正确功能.如果我想要字符串的位置,我调用`strpos()`.如果我想在该位置之后的子串,我调用`strstr()`. (10认同)