_mb_ord()WP 7.1.0

Internal compat function to mimic mb_ord().

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Returns

int|false. The Unicode code point for the first character of string or false on failure.

Usage

_mb_ord( $string, $encoding );
$string(string) (required)
Return the code point at the start of this string.
$encoding("UTF-8"|null)
Must be 'UTF-8' or null.
Default: null

Changelog

Since 7.1.0 Introduced.

_mb_ord() code WP 7.1

function _mb_ord( $string, $encoding = null ) {
	if ( ! is_string( $string ) || '' === $string || ( isset( $encoding ) && 'UTF-8' !== $encoding ) ) {
		return false;
	}

	$byte_length    = 0;
	$invalid_length = 0;
	$found_count    = _wp_scan_utf8( $string, $byte_length, $invalid_length, null, 1 );

	if ( 1 !== $found_count ) {
		return false;
	}

	// These are valid code points, so no further validation is required.
	$b0 = ord( $string[0] );

	switch ( $byte_length ) {
		case 1:
			return $b0;

		case 2:
			return (
				( ( $b0 & 0x1F ) << 6 ) |
				( ( ord( $string[1] ) & 0x3F ) )
			);

		case 3:
			return (
				( ( $b0 & 0x0F ) << 12 ) |
				( ( ord( $string[1] ) & 0x3F ) << 6 ) |
				( ( ord( $string[2] ) & 0x3F ) )
			);

		case 4:
			return (
				( ( $b0 & 0x07 ) << 18 ) |
				( ( ord( $string[1] ) & 0x3F ) << 12 ) |
				( ( ord( $string[2] ) & 0x3F ) << 6 ) |
				( ( ord( $string[3] ) & 0x3F ) )
			);
	}

	return false;
}