PHP 8 中的 str_starts_with 和 str_ends_with 函数
在PHP8中添加了str_starts_with和str_ends_with函数来检查给定的字符串是否以另一个字符串开头或结尾。如果它以另一个字符串开头和结尾,则返回true,否则返回false。
例子
str_starts_with('hello haystack', 'hello'); //开始字符串找到“真” str_starts_with('hello haystack', 'stack'); //ends string found 'True'
str_starts_with('hello haystack', 'hay'); //开始字符串找到“假” str_starts_with('hello haystack', 'hay'); //ends string found 'False'
str_starts_with()PHP8中的函数
此函数检查给定的字符串是否以字符串针开头。如果找到第一个字符串,则返回true,否则返回false。
str_starts_with(string $haystack, string $needle): bool
示例:使用str_starts_with()函数。
输出
String starts with 'hello'
注意:如果在第二个字符串中找不到给定的第一个起始字符串,则返回false。
str_ends_with()PHP8中的函数
此函数检查给定的字符串是否以字符串针结尾。如果找到给定的字符串结尾,则返回true,否则返回false。
str_ends_with(string $haystack, string $needle):bool
示例:使用str_ends_with()函数
输出
String ends with 'stack'