WordPress函数:wp_generate_uuid4 和 wp_is_uuid 生成和判断UUID
UUID 经常作为唯一标识使用。WordPress也提供了相应的函数。
wp_generate_uuid4 生成一个UUID。
函数源码:
function wp_generate_uuid4() {
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0x0fff ) | 0x4000,
mt_rand( 0, 0x3fff ) | 0x8000,
mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff ),
mt_rand( 0, 0xffff )
);
}
使用举例:
$uuid = wp_generate_uuid4();
wp_is_uuid 判断一个字符串是否是UUID。
函数源码:
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 );
}
可以看出,$version支持null或者4,也就是 wp_generate_uuid4 生成的UUID。
-
WordPress如何在指定文章中不显示短代码WordPress若想在指定文章中不显示短代码,可以按追格小编的方法操作。
-
WordPress函数:category_exists 判断分类是否存在在WordPress中 如何判断一个分类是否存在
-
WordPress函数:email_exists 判断邮箱是否已注册为用户在WordPress中 如何判断一个邮箱是否已注册为用户
-
WordPress插件:Fixed Widget小工具跟随滚动选项插件Fixed Widget是一款让指定的小工具跟随页面滚动,始终保持在屏幕可见区域中的WordPress插件。
-
WordPress函数:register_new_user 和 wp_insert_user 创建新用户的区别WordPress函数:register_new_user 和 wp_insert_user 创建新用户的区别
-
WordPress函数:get_the_guid 和 get_comment_guid 文章和评论的唯一标识WordPress中的 guid 简介
暂无评论,抢个沙发...