Current Location: Home> Function Categories> str_contains

str_contains

Determines whether a string contains a given substring
Name:str_contains
Category:String
Programming Language:php
One-line Description:Check if a string contains another string

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.
  • In versions before PHP 8.0.0, you can use the 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.
Similar Functions
Popular Articles