before_last_bar( string $text ): string

Removes last item on a pipe-delimited string.

Description

Meant for removing the last item in a string, such as ‘Role name|User role’. The original string will be returned if no pipe ‘|’ characters are found in the string.

Parameters

$textstringrequired
A pipe-delimited string.

Return

string Either $text or everything before the last pipe.

Source

function before_last_bar( $text ) {
	$last_bar = strrpos( $text, '|' );
	if ( false === $last_bar ) {
		return $text;
	} else {
		return substr( $text, 0, $last_bar );
	}
}

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

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