str_ends_with()WP 5.9.0

Performs a case-sensitive check indicating if the haystack ends with needle.

Performs a case-sensitive check indicating if the haystack ends with needle.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.01 sec (speed of light)

No Hooks.

Return

true|false. True if $haystack ends with $needle, otherwise false.

Usage

str_ends_with( $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

str_ends_with( 'ABC', 'C' ); // true

str_ends_with( 'ABC', 'A' ); // false

// case sensitive
str_ends_with( 'ABC', 'c' ); // false

// all lines end with an empty string
str_ends_with( 'abc', '' ); // true

Changelog

Since 5.9.0 Introduced.

str_ends_with() code WP 6.5.2

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

	$len = strlen( $needle );

	return substr( $haystack, -$len, $len ) === $needle;
}