123456789101112131415161718192021222324252627282930313233343536373839 |
- <?php
- namespace app\helpers;
- use yii\helpers\ArrayHelper;
- class Uuid
- {
-
-
- public static function GetUUID():string
- {
- $bytes = random_bytes(16);
- $bytes[6] = chr((ord($bytes[6]) & 0x0f) | 0x40);
- $bytes[8] = chr((ord($bytes[8]) & 0x3f) | 0x80);
- $hex = bin2hex($bytes);
- $fields = [
- 'time_low' => substr($hex, 0, 8),
- 'time_mid' => substr($hex, 8, 4),
- 'time_hi_and_version' => substr($hex, 12, 4),
- 'clock_seq_hi_and_reserved' => substr($hex, 16, 2),
- 'clock_seq_low' => substr($hex, 18, 2),
- 'node' => substr($hex, 20, 12),
- ];
- return vsprintf(
- '%08s-%04s-%04s-%02s%02s-%012s',
- $fields
- );
- }
- public static function isvalid(string $string):string
- {
- if( preg_match('/^([a-z0-9]){8}\-([a-z0-9]){4}\-([a-z0-9]){4}\-([a-z0-9]){4}\-([a-z0-9]){12}$/', $string) ){
- return true;
- }
- return false;
- }
- }
|