str_ends_with
( str_starts_with
)的作用:检查字符串是否以给定的子字符串结尾(开头)
PHP8
里面有自带的函数,可以直接使用
我想着 PHP8
以下的都没有,那不行,得加上。
php7
主要逻辑是获取字符串来判断和预查询的字符串是否相同。
其中 mb_substr($str,$start,$length,encoding=mb_internal_encoding())
根据字符数执行一个多字节安全的 substr() 操作。 位置是从 str 的开始位置进行计数。 第一个字符的位置是 0。第二个字符的位置是 1,以此类推。
下面是我的示例代码
$a = '品斯顿dfasdfasdfas';
function startWith($haystack,$needle){
$len = mb_strlen($needle);
return mb_substr($haystack, 0, $len) === $needle;
}
function endWith($haystack,$needle){
$len = mb_strlen($needle);
return mb_substr($haystack, -1, $len) === $needle;
}
// 输出:bool(true)
var_dump( startWith($a,'品') );