(PHP 8)
str_starts_with — 檢查字串是否以給定的子字串開頭
haystack(乾草堆)
要在其中搜尋的字串。
needle(針)
要在 haystack
中搜尋的子字串。
範例 #1 使用空字串 ''
<?php
if (str_starts_with('abc', '')) {
echo "所有字串都以空字串開頭";
}
?>
上述範例將輸出
All strings start with the empty string
範例 #2 顯示區分大小寫
<?php
$string = '懶惰的狐狸跳過柵欄';
if (str_starts_with($string, '懶惰的')) {
echo "字串以 '懶惰的' 開頭\n";
}
if (str_starts_with($string, '的')) {
echo '字串以 "的" 開頭';
} else {
echo '"的" 找不到,因為大小寫不符';
}
?>
上述範例將輸出
The string starts with 'The' "the" was not found because the case does not match
注意:此函式為二進位安全。