PHP generates UUID format strings

Source: Internet
Author: User
Tags chr uuid

A UUID is a number generated on a single machine that guarantees that all machines in the same space-time are unique. Typically, the platform provides APIs for generating UUID. The UUID, based on the standard calculations developed by the Open Software Foundation (OSF), uses Ethernet card addresses, nanosecond time, chip ID codes, and many possible numbers. A combination of the following parts: The current date and time (the first part of the UUID is related to time, and if you generate a UUID after a few seconds after generating a UUID, the first part is different, the rest is the same), the clock sequence, and the globally unique IEEE Machine identification number (if there is a NIC, obtained from the NIC, No NIC is available in any other way, the only drawback of the UUID is that the resulting string will be relatively long. The most common use of the standard for UUID is the Microsoft GUID (Globals Unique Identifiers).

In ColdFusion, you can use the Createuuid () function to easily generate a UUID in the form of: Xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx (8-4-4-16), where each x is 0-9 or a-f A hexadecimal number within the range. The standard UUID format is: xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)

  code is as follows &nbs P;

<?php 
Function guid () { 
    I F (function_exists (' Com_create_guid ')) { 
        return Com_create_ GUID ();  
   }else{ 
        Mt_srand (double) Microtime () *10000);
//optional for PHP 4.2.0 and up. 
        $charid = Strtoupper (MD5 ( Uniqid (rand (), true))  
        $hyphen = Chr ();
//"-" &NBSP;
& nbsp;       $uuid = chr (123)
//"{" &NBSP;
                 substr ($charid, 0, 8). $hyphen
                . substr ($charid, 8, 4). $hyphen
                 substr ($charid, 4). $hyphen
                 substr ($charid, 4). $hyphen
                 substr ($charid, 20,12)  
                chr (125);
//"}" &NBSP;
        return $uuid;  
   } 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.