PHP 版本的startsWith 和 endsWith

JS处理字符串的时候,有些地方还是很方便的。

但是PHP也不是很逊色,也有对应的解决方案。

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

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

不理解strcmpsubstr_compare的可以自己去查查文档

CC BY-NC-ND 4.0