实现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/

猜你喜欢

转载自www.cnblogs.com/tommy-huang/p/9357631.html