php startsWith,endsWith

php并没有内置startsWith,endsWith这两个函数,以下是一个实现

 

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

function endsWith($haystack, $needle) {
    $length = strlen($needle);
 $start =  $length *-1; //negative
 return (substr($haystack, $start, $length) === $needle);
}

你可能感兴趣的:(PHP)