str_starts_with
检查字符串是否以给定的子字符串开头
PHP 8.0.0 及以上版本
str_starts_with 函数用于检查一个字符串是否以指定的子字符串开头。
bool str_starts_with(string $haystack, string $needle)
如果 $haystack 以 $needle 开头,函数返回 true;否则返回 false。
以下是使用 str_starts_with 函数的示例:
$haystack = "Hello, world!"; $needle = "Hello"; <p>if (str_starts_with($haystack, $needle)) {<br> echo "字符串以 '$needle' 开头";<br> } else {<br> echo "字符串不以 '$needle' 开头";<br> }<br>
在这个示例中,$haystack 是要检查的字符串 "Hello, world!",$needle 是我们希望它以 "Hello" 开头的子字符串。因为 "Hello, world!" 确实以 "Hello" 开头,函数将返回 true,因此输出 "字符串以 'Hello' 开头"。