note 104868 deleted from function.uniqid by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 Jul 2011 17:03:06 +0000 |
| Subject: | note 104868 deleted from function.uniqid by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181478@lists.php.net to get a copy of this message | ||
Note Submitter: mmw at example dot com
----
function uuidgen($sha1 = false, $exec = true) {
$stdout = array();
$cmd = "uuidgen 2>&1";
$status = -100;
if ($exec) {
exec($cmd, $stdout, $status);
}
if ($status == 0 && count($stdout)) {
$uuid = $stdout[0];
} else {
$md5 = strtoupper(md5(uniqid(microtime(true), true)));
$uuid = sprintf("%04s%04s-%04s-%04s-%04s-%04s%04s%04s",
$md5[0].$md5[1].$md5[2].$md5[3],
$md5[4].$md5[5].$md5[6].$md5[7],
$md5[8].$md5[9].$md5[10].$md5[11],
$md5[12].$md5[13].$md5[14].$md5[15],
$md5[16].$md5[17].$md5[18].$md5[19],
$md5[20].$md5[21].$md5[22].$md5[23],
$md5[24].$md5[25].$md5[26].$md5[27],
$md5[28].$md5[29].$md5[30].$md5[31]
);
}
if ($sha1) {
$uuid = sha1($uuid);
}
return $uuid;
}
a simple thought which may satisfy many of you 8-), the md5 char array; just to be verbose 8-)