str_ends_with()WP 5.9.0

Checks if the string ends with the specified substring. Case sensitive.

This is a polyfill for the new PHP function str_ends_with() introduced in PHP version 8.0.

See also similar functions:

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

No Hooks.

Returns

true|false. True if the string ($haystack) ends with the substring ($needle), false otherwise.

Usage

str_ends_with( $haystack, $needle );
$haystack(string) (required)
Hay - the string we are checking.
$needle(string) (required)
Needle - the substring that should be at the end of $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 7.0

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

	$len = strlen( $needle );

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