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

后端 未结 13 821
时光说笑
时光说笑 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:25

    In PHP 8:

    str_ends_with('haystack', 'stack'); // true
    str_ends_with('haystack', 'K'); // false
    

    and also:

    str_starts_with('haystack', 'hay'); // true
    

    PHP RFC: Add str_starts_with(), str_ends_with() and related functions

提交回复
热议问题