What's the most efficient test of whether a PHP string ends with another string?

后端 未结 13 777
时光说笑
时光说笑 2020-11-29 18:53

The standard PHP way to test whether a string $str ends with a substring $test is:

$endsWith = substr( $str, -strlen( $test ) ) ==          


        
13条回答
  •  死守一世寂寞
    2020-11-29 19:34

    Don't know if this is fast or not but for a single character test, these work, too:

    (array_pop(str_split($string)) === $test) ? true : false;
    ($string[strlen($string)-1] === $test) ? true : false;
    (strrev($string)[0] === $test) ? true : false;
    

提交回复
热议问题