php开发中,判断字符串中是否包含某个字符,一般用以下几个函数。

  • strpos
  • stripos
  • preg_match
  • php8新增:
  • str_starts_with
  • str_ends_with
  • str_contains

测试结果

## 1000次
- str_contains    :0.000079870223999 秒
- str_starts_with :0.000073909759521 秒
- str_ends_with   :0.000077962875366 秒
- strpos          :0.000090837478638 秒
- stripos         :0.000122785568237 秒
- preg_match      :0.000221014022827 秒

## 10000次
- str_contains    :0.000777959823608 秒
- str_starts_with :0.000720024108887 秒
- str_ends_with   :0.000742197036743 秒
- strpos          :0.000861883163452 秒
- stripos         :0.001212120056152 秒
- preg_match      :0.002089977264404 秒

## 100000次
- str_contains    :0.007513046264648 秒
- str_starts_with :0.007155895233154 秒
- str_ends_with   :0.007284879684448 秒
- strpos          :0.007875919342041 秒
- stripos         :0.011719942092896 秒
- preg_match      :0.021173000335693 秒

## 500000次
- str_contains    :0.035715103149414 秒
- str_starts_with :0.033644914627075 秒
- str_ends_with   :0.034588098526001 秒
- strpos          :0.040426015853882 秒
- stripos         :0.057986021041870 秒
- preg_match      :0.105597972869873 秒

## 1000000次
- str_contains    :0.070344924926758 秒
- str_starts_with :0.065416097640991 秒
- str_ends_with   :0.067116022109985 秒
- strpos          :0.080109119415283 秒
- stripos         :0.114392995834351 秒
- preg_match      :0.211956977844238 秒

## 5000000次
- str_contains    :0.354083061218262 秒
- str_starts_with :0.322311878204346 秒
- str_ends_with   :0.333225011825562 秒
- strpos          :0.400656938552856 秒
- stripos         :0.568760871887207 秒
- preg_match      :1.054600000381470 秒

## 10000000次
- str_contains    :0.705474138259888 秒
- str_starts_with :0.648499011993408 秒
- str_ends_with   :0.663192033767700 秒
- strpos          :0.792457103729248 秒
- stripos         :1.148205041885376 秒
- preg_match      :2.158888101577759 秒

测试代码

function benchmark($func, $text, $needle, $iterations) {
    $start = microtime(true);
    for ($i = 0; $i < $iterations; $i++) {
        $func($text, $needle);
    }
    return microtime(true) - $start; // 秒
}

$text = 'The quick brown fox jumps over the lazy dog.';
$needle = 'fox';

$tests = [
    'str_contains    ' => fn($text, $needle) => str_contains($text, $needle),
    'str_starts_with ' => fn($text, $needle) => str_starts_with($text, $needle),
    'str_ends_with   ' => fn($text, $needle) => str_ends_with($text, $needle),
    'strpos          ' => fn($text, $needle) => strpos($text, $needle) !== false,
    'stripos         ' => fn($text, $needle) => stripos($text, $needle) !== false,
    'preg_match      ' => fn($text, $needle) => preg_match('/' . preg_quote($needle, '/') . '/', $text),
];

$iterationsList = [
    1000,
    10000,
    100000,
    500000,
    1000000,
    5000000,
    10000000
];
echo '<pre>';
foreach ($iterationsList as $n) {
    echo "## {$n}次\n";
    foreach ($tests as $name => $func) {
        $time = benchmark($func, $text, $needle, $n);
        // 显示完整小数,不用科学计数法
        echo "- {$name}:" . sprintf('%.15f', $time) . " 秒\n";
    }
    echo "\n";
}
echo '</pre>';