函数名称:str_starts_with()
函数功能:判断一个字符串是否以指定的前缀开始。
适用版本:PHP 8.0.0 及以上版本。
语法:bool str_starts_with ( string $haystack , string $needle )
参数:
返回值:
示例:
$haystack = "Hello, world!";
$needle = "Hello";
if (str_starts_with($haystack, $needle)) {
echo "字符串以指定前缀开始";
} else {
echo "字符串不以指定前缀开始";
}
输出:
字符串以指定前缀开始
注意事项:
substr($haystack, 0, strlen($needle)) === $needle
。