str_contains
Determines whether a string contains a given substring
Function name: str_contains()
Function Description: This function is used to check whether a string contains another string.
Applicable version: PHP 8.0.0 and above
usage:
bool str_contains ( string $haystack , string $needle )
parameter:
$haystack
: The string to search for.$needle
: The substring to search for. Return value: true
if $haystack
contains $needle
, otherwise false
.
Example:
$str = "Hello, world!"; $substring = "world"; if (str_contains($str, $substring)) { echo "字符串中包含子字符串"; } else { echo "字符串中不包含子字符串"; }
Output:
字符串中包含子字符串
Notes:
str_contains()
function is case-sensitive. If you need to do case-insensitive searches, you can use the stripos()
function.strpos()
function to check whether a string contains another string, but you need to manually determine whether the return value is false
. After PHP 8.0.0, it is recommended to use str_contains()
function because it is more concise and intuitive.