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函数:国际化 _nx 翻译字符串,_n + _xWordPress函数:国际化 _nx 翻译字符串,_n + _x
-
WordPress函数:_n_loop 在 pot 文件中注册字符串WordPress函数:_n_loop 在 pot 文件中注册字符串
-
WordPress函数:国际化 _nx_noop 在 pot 中注册字符串WordPress函数:国际化 _nx_noop 在 pot 中注册字符串
-
WordPress 6.5移除区块样板菜单项的方法WordPress 6.5新增的区块样板菜单项,可将不同区块组合成一个新的区块模板,方便重复使用。
-
WordPress做多语言企业网站近期常用小伙伴问追格小编,有没有计划做双语WordPress企业官网主题。
-
WP Multilang WordPress翻译插件WP Multilang插件的主要功能是提供多语言支持,使用户能够创建多语言版本的网站,满足不同语言用户的需求。
暂无评论,抢个沙发...