PHP UUID generator function

Via: http://www.ajaxray.com/blog/2008/02/06/php-uuid-generator-function/

/**
  * Generates an UUID
  *
  * @author     Anis uddin Ahmad 
  * @param      string  an optional prefix
  * @return     string  the formatted uuid
  */
  function uuid($prefix = ”)
  {
    $chars = md5(uniqid(rand()));
    $uuid  = substr($chars,0,8) . '-';
    $uuid .= substr($chars,8,4) . '-';
    $uuid .= substr($chars,12,4) . '-';
    $uuid .= substr($chars,16,4) . '-';
    $uuid .= substr($chars,20,12);

    return $prefix . $uuid;
  }

Author: Jeff

Born a cantankerous old man, mellowed ever so slightly by age.

2 thoughts on “PHP UUID generator function”

Leave a Reply

Your email address will not be published. Required fields are marked *