str_ends_with
检查字符串是否以给定的子字符串结尾
PHP 8.0.0 及以上版本
str_ends_with 函数用于检查一个字符串是否以指定的子字符串结尾。
str_ends_with(string $haystack, string $needle): bool
$haystack (string): 需要检查的字符串。
$needle (string): 要检查的子字符串。
返回布尔值 true 如果字符串 $haystack 以子字符串 $needle 结尾,否则返回 false。
以下是使用 str_ends_with 函数的示例:
$string1 = "Hello, World!"; $string2 = "World"; if (str_ends_with($string1, $string2)) { echo "字符串 '$string1' 以 '$string2' 结尾。"; } else { echo "字符串 '$string1' 不以 '$string2' 结尾。"; }
在此示例中,我们定义了两个字符串变量 $string1 和 $string2。我们使用 str_ends_with 来检查 $string1 是否以 $string2 结尾。如果是,输出提示字符串以该子字符串结尾;否则输出字符串不以该子字符串结尾。