str_starts_with
檢查字符串是否以給定的子字符串開頭
函數名稱: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
。