str_contains()WP 5.9.0

Performs a case-sensitive check indicating if needle is contained in haystack.

Performs a case-sensitive check indicating if needle is contained in haystack.

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.

Return

true|false. True if $needle is in $haystack, otherwise false.

Usage

str_contains( $haystack, $needle );
$haystack(string) (required)
The string to search in.
$needle(string) (required)
The substring to search for in the $haystack.

Examples

0

#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() code WP 6.5.2

function str_contains( $haystack, $needle ) {
	if ( '' === $needle ) {
		return true;
	}

	return false !== strpos( $haystack, $needle );
}