Skip to content

Instantly share code, notes, and snippets.

@goocarlos
Created July 24, 2014 06:54
Show Gist options
  • Save goocarlos/5a6fce9e2ff0697e60a4 to your computer and use it in GitHub Desktop.
Save goocarlos/5a6fce9e2ff0697e60a4 to your computer and use it in GitHub Desktop.
<?php
/**
* 生成一个 $l 位的 62 进制随机字符串, 其第一位字符不为数字
* @author Luyu Zhang<goocarlos@gmail.com>
*/
function mt_rand_base62($l) {
$c = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
for ($s = '', $cl = strlen($c)-1, $i = 0; $i < $l; $s .= $c[mt_rand(0, $cl)], ++$i);
$s[0] = is_numeric($s[0])?$letters[mt_rand(0, strlen($letters)-1)]:$s[0];
return $s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment