实现php的startsWith和endsWith

startsWith():

function startsWith($haystack, $needle){
    return strncmp($haystack, $needle, strlen($needle)) === 0;
}

 

endsWith():

function endsWith($haystack, $needle){
    return $needle === '' || substr_compare($haystack, $needle, -strlen($needle)) === 0;
}

 

参考:https://www.gowhich.com/blog/747
           https://leonax.net/p/7804/string-startswith-and-endswith-in-php/

posted @ 2018-07-23 23:47  一菲聪天  阅读(3000)  评论(0编辑  收藏  举报