wp_is_uuid()WP 4.9.0

Checks if the specified string is a UUID string.

You can create a UUID string with function wp_generate_uuid4().

1 time — 0.000104 sec (fast) | 50000 times — 0.02 sec (speed of light) | PHP 7.1.5, WP 4.9.4

No Hooks.

Returns

true|false. If the string is a UUID, it will return true, otherwise it will return false.

Usage

wp_is_uuid( $uuid, $version );
$uuid(string) (required)
UUID to check.
$version(number)
The version of the UUID. If not specified, any version of the UUID is checked. If specified, only version 4 is allowed. If you try to specify another version, the message 'Only UUID V4 is supported at this time' will be displayed.
Default: null

Examples

0

#1 Check the UUID version 4 [auto-translate]

$uuid = '4c585b5e-5220-4b1d-92e2-316f88210482';

if( wp_is_uuid($uuid) ){
	echo 'This is the UUID string;
}
0

#2 UUID must be lowercase only

wp_is_uuid( '4c585b5e-5220-4b1d-92e2-316f88210482' ); // bool(true)
wp_is_uuid( '4C585B5E-5220-4B1D-92E2-316F88210482' ); // bool(false)

Changelog

Since 4.9.0 Introduced.

wp_is_uuid() code WP 6.4.3

function wp_is_uuid( $uuid, $version = null ) {

	if ( ! is_string( $uuid ) ) {
		return false;
	}

	if ( is_numeric( $version ) ) {
		if ( 4 !== (int) $version ) {
			_doing_it_wrong( __FUNCTION__, __( 'Only UUID V4 is supported at this time.' ), '4.9.0' );
			return false;
		}
		$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/';
	} else {
		$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/';
	}

	return (bool) preg_match( $regex, $uuid );
}