PHP验证有效的UUID [英] PHP verify valid UUID

查看:130
本文介绍了PHP验证有效的UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一个答案中找到了以下 Javascript 函数:

I found the following Javascript function in another answer:

function createUUID() {
    var s = [];
    var hexDigits = "0123456789abcdef";

    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }

    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}

这会在 javascript 中创建一个 RFC 有效的 UUID/GUID.我想知道的是,一旦字符串到达​​ PHP 端,是否有办法验证它.我发现了一个正则表达式,它可能会大致验证该格式的所有内容为真:

This creates an RFC valid UUID/GUID in javascript.. What I want to know is if there is a way to validate the string once it arrives to the PHP side. I found a regex that would potentially validate everything roughly that format as true:

/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/

但是只要你指定一个长度和格式相等的字符串,它就会通过验证.

but using that as long as you specified a string of equal length and format it would pass validation.

有没有办法向 javascript 函数添加某种种子,然后在 PHP 中验证它,或者是否有一种方法可以让 PHP 验证传递的数字是否与正确的格式匹配?

Is there any way to add some sort of seed to the javascript function and then verify that in the PHP or if there was a way for PHP to validate the number that was passed matches with the correct format or something?

非常感谢任何帮助.

推荐答案

根据 维基百科UUID v4 的格式为:

According to Wikipedia the format of UUID v4 is:

第 4 版 UUID 的格式为 xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx其中 x 是任何十六进制数字,y 是 8、9、A 或 B 之一.例如f47ac10b-58cc-4372-a567-0e02b2c3d479.

Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B. e.g. f47ac10b-58cc-4372-a567-0e02b2c3d479.

对应的正则表达式为:

/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i

这篇关于PHP验证有效的UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆