温馨提示×

php str_contains检查字符串的技巧

PHP
小樊
82
2024-07-05 04:06:19
栏目: 编程语言

PHP中的str_contains函数用于检查一个字符串中是否包含另一个字符串,并返回一个布尔值。这个函数在PHP 8.0版本中新增,用于替代strpos函数,更加直观和易于理解。

下面是一个使用str_contains函数的示例:

$string = 'Hello, world!';
$substring = 'world';

if (str_contains($string, $substring)) {
    echo "The string contains the substring.";
} else {
    echo "The string does not contain the substring.";
}

在上面的示例中,我们首先定义了一个包含字符串的变量$string和一个子字符串的变量$substring。然后使用str_contains函数检查$string中是否包含$substring,并根据结果输出相应的信息。

需要注意的是,str_contains函数是区分大小写的,如果需要不区分大小写,可以使用stripos函数来替代。

0