str_contains()
Checks if the specified string (substring) is in another string. Case sensitive.
This is a polyfill for the new php function str_contains() introduced in PHP version 8.0.
See also similar functions:
1 time — 0.000001 sec (speed of light) | 50000 times — 0.0001 sec (speed of light) | PHP 7.4.25, WP 5.9
No Hooks.
Returns
true|false. True if the substring ($needle) is found in the string ($haystack), false otherwise.
Usage
str_contains( $haystack, $needle );
- $haystack(string) (required)
- Hay - the string in which to find the substring.
- $needle(string) (required)
- Needle - the substring to find in the string.
Examples
#1 Demo of work
str_contains( 'ABC', 'B' ); // true // case sensitivity str_contains( 'ABC', 'c' ); // false // empty (string) is always in any string str_contains( 'abc', '' ); // true
Changelog
| Since 5.9.0 | Introduced. |
str_contains() str contains code WP 7.0
function str_contains( $haystack, $needle ) {
if ( '' === $needle ) {
return true;
}
return false !== strpos( $haystack, $needle );
}