WP_Token_Map::longest_first_then_alphabetical( string $a, string $b ): int

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Compares two strings, returning the longest, or whichever is first alphabetically if they are the same length.

Description

This is an important sort when building the token map because it should not form a match on a substring of a longer potential match. For example, it should not detect Cap when matching against the string CapitalDifferentialD.

Parameters

$astringrequired
First string to compare.
$bstringrequired
Second string to compare.

Return

int -1 or lower if $a is less than $b; 1 or greater if $a is greater than $b, and 0 if they are equal.

Source

 * @return int -1 or lower if `$a` is less than `$b`; 1 or greater if `$a` is greater than `$b`, and 0 if they are equal.
 */
private static function longest_first_then_alphabetical( string $a, string $b ): int {
	if ( $a === $b ) {
		return 0;
	}

	$length_a = strlen( $a );
	$length_b = strlen( $b );

	// Longer strings are less-than for comparison's sake.
	if ( $length_a !== $length_b ) {
		return $length_b - $length_a;
	}

Changelog

VersionDescription
6.6.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.