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

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

    I hope that the below answer may be efficient and also simple:

    $content = "The main string to search";
    $search = "search";
    //For compare the begining string with case insensitive. 
    if(stripos($content, $search) === 0) echo 'Yes';
    else echo 'No';
    
    //For compare the begining string with case sensitive. 
    if(strpos($content, $search) === 0) echo 'Yes';
    else echo 'No';
    
    //For compare the ending string with case insensitive. 
    if(stripos(strrev($content), strrev($search)) === 0) echo 'Yes';
    else echo 'No';
    
    //For compare the ending string with case sensitive. 
    if(strpos(strrev($content), strrev($search)) === 0) echo 'Yes';
    else echo 'No';
    

提交回复
热议问题