<?
//지정된 자릿수의 랜덤한 숫자를 반환합니다. 최대 10까지 가능합니다. 4 이면 1000 에서 9999 사이의 랜덤 숫자
function get_rand_number($len=4) {
if (empty($len) || !is_int($len)) $len = 4;
else if ($len < 0) $len = abs($len);
if ($len > 10) $len = 10;
return rand(bcpow(10, $len - 1), bcadd(bcpow(10, $len), -1));
}
//지정된 자릿수의 숫자로된 시리얼을 반환합니다. - 를 포함하고 싶지 않을때는 $cut 이 $len 보다 크거나 같으면 됩니다.
function get_serial($len=16, $cut=4, $hipen='-'){
if (empty($len) || !is_int($len)) $len = 16;
else if ($len < 0) $len = abs($len);
if (empty($cut) || !is_int($cut)) $cut = 4;
else if ($cut < 0) $cut = abs($cut);
list($usec, $sec) = explode(' ', microtime());
$serial = str_replace('.', '', (string)bcmul(bcmul((float)$usec * 1000000 , (float)$sec) , get_rand_number(4)));
$serial_length = strlen($serial);
$sub = $len - $serial_length;
if ($sub > 0) $serial .= (string)get_rand_number($sub);
else if ($sub < 0) $serial = substr($serial, 0, $len);
return preg_replace("`(.{" . $cut . "})`", "$1" . $hipen, $serial, floor(($len-1) / $cut));
}
//넘어온 세자리수를 36진수로 변환
function get_simple_36($m){
$str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$div = floor($m[0] / 36);
$rest = $m[0] % 36;
return $str[$div] . $str[$rest];
}
//지정된 자릿수의 숫자와 영문으로된 시리얼을 반환합니다. - 를 포함하고 싶지 않을때는 $cut 이 $len 보다 크거나 같으면 됩니다.
function get_serial_mix($len=16, $cut=4, $hipen='-'){
if (empty($len) || !is_int($len)) $len = 16;
else if ($len < 0) $len = abs($len);
if (empty($cut) || !is_int($cut)) $cut = 4;
else if ($cut < 0) $cut = abs($cut);
$len2 = (int)($len * 3 / 2);
if ($len2 % 2 == 1) $len2 += 1;
$serial = get_serial($len2, $len2, $hipen);
$serial = substr(preg_replace_callback("`.{3}`", "get_simple_36", $serial), 0, $len);
return preg_replace("`(.{" . $cut . "})`", "$1" . $hipen, $serial, floor(($len-1) / $cut));
}
$serial = get_serial_mix(16, 4);
echo $serial;
?>
이전글의 코멘트에서 전진님이 지적해준 세가지 문제를 수정하였습니다.
좋은 지적을 해주신 전진님께 감사드립니다.
댓글 4개
13년 전
쿨하게 받아주신 유창화님께 감사드립니다. ^^
13년 전
저도 아직 많이 부족한 사람이에요
좋은 지적 고맙습니다.
좋은 지적 고맙습니다.
13년 전
생성된 결과가 숫자보다 알파벳이 더 많게 하기 위한 간단한 방법
return $str[$div] . $str[$rest];
을
return $str[(36-$div)] . $str[(36-$rest)];
로 만 바꿔주면 됩니다.
물론 문자열을 아예 섞어버리는것도 가능합니다.
return $str[$div] . $str[$rest];
을
return $str[(36-$div)] . $str[(36-$rest)];
로 만 바꿔주면 됩니다.
물론 문자열을 아예 섞어버리는것도 가능합니다.
13년 전
오.. 이런 트윅 너무 좋아합니다. ^^;
고맙습니다. ^^
고맙습니다. ^^
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5330 | 13년 전 | 759 | ||
| 5329 | 13년 전 | 698 | ||
| 5328 | 13년 전 | 636 | ||
| 5327 | 13년 전 | 879 | ||
| 5326 |
downmix
|
13년 전 | 1116 | |
| 5325 |
techer
|
13년 전 | 3139 | |
| 5324 |
techer
|
13년 전 | 963 | |
| 5323 | 13년 전 | 1174 | ||
| 5322 |
김준수사랑
|
13년 전 | 889 | |
| 5321 |
다케미카코
|
13년 전 | 749 | |
| 5320 |
|
13년 전 | 994 | |
| 5319 |
한번잘해보자
|
13년 전 | 1000 | |
| 5318 |
|
13년 전 | 1264 | |
| 5317 |
techer
|
13년 전 | 2698 | |
| 5316 |
techer
|
13년 전 | 3140 | |
| 5315 | 13년 전 | 1170 | ||
| 5314 |
티즈코리아
|
13년 전 | 1226 | |
| 5313 | 13년 전 | 697 | ||
| 5312 |
|
13년 전 | 1865 | |
| 5311 |
innis
|
13년 전 | 761 | |
| 5310 | 13년 전 | 851 | ||
| 5309 |
changho
|
13년 전 | 659 | |
| 5308 |
|
13년 전 | 1361 | |
| 5307 |
|
13년 전 | 791 | |
| 5306 | 13년 전 | 1159 | ||
| 5305 |
세상속으로
|
13년 전 | 1148 | |
| 5304 | 13년 전 | 657 | ||
| 5303 | 13년 전 | 2237 | ||
| 5302 | 13년 전 | 1385 | ||
| 5301 | 13년 전 | 1104 | ||
| 5300 |
프로프리랜서
|
13년 전 | 1075 | |
| 5299 | 13년 전 | 858 | ||
| 5298 | 13년 전 | 674 | ||
| 5297 |
나랑사귈래
|
13년 전 | 608 | |
| 5296 | 13년 전 | 1336 | ||
| 5295 | 13년 전 | 1118 | ||
| 5294 | 13년 전 | 729 | ||
| 5293 |
|
13년 전 | 5820 | |
| 5292 |
|
13년 전 | 792 | |
| 5291 | 13년 전 | 613 | ||
| 5290 | 13년 전 | 553 | ||
| 5289 | 13년 전 | 651 | ||
| 5288 | 13년 전 | 1163 | ||
| 5287 | 13년 전 | 758 | ||
| 5286 | 13년 전 | 967 | ||
| 5285 | 13년 전 | 1323 | ||
| 5284 |
미스홍당무
|
13년 전 | 853 | |
| 5283 |
basketball
|
13년 전 | 1745 | |
| 5282 | 13년 전 | 783 | ||
| 5281 |
|
13년 전 | 793 | |
| 5280 | 13년 전 | 1207 | ||
| 5279 | 13년 전 | 1374 | ||
| 5278 |
크라이스트
|
13년 전 | 2045 | |
| 5277 |
|
13년 전 | 731 | |
| 5276 |
그누보드환자이송
|
13년 전 | 788 | |
| 5275 | 13년 전 | 703 | ||
| 5274 | 13년 전 | 1372 | ||
| 5273 | 13년 전 | 2004 | ||
| 5272 | 13년 전 | 1119 | ||
| 5271 | 13년 전 | 1434 | ||
| 5270 | 13년 전 | 808 | ||
| 5269 | 13년 전 | 521 | ||
| 5268 | 13년 전 | 643 | ||
| 5267 | 13년 전 | 1180 | ||
| 5266 | 13년 전 | 1838 | ||
| 5265 | 13년 전 | 1608 | ||
| 5264 |
크라이스트
|
13년 전 | 1197 | |
| 5263 |
|
13년 전 | 1263 | |
| 5262 |
제이티37
|
13년 전 | 583 | |
| 5261 |
|
13년 전 | 776 | |
| 5260 |
quenya
|
13년 전 | 2069 | |
| 5259 | 13년 전 | 705 | ||
| 5258 | 13년 전 | 725 | ||
| 5257 |
크라이스트
|
13년 전 | 4592 | |
| 5256 | 13년 전 | 1249 | ||
| 5255 | 13년 전 | 3253 | ||
| 5254 |
|
13년 전 | 703 | |
| 5253 | 13년 전 | 768 | ||
| 5252 | 13년 전 | 766 | ||
| 5251 | 13년 전 | 713 | ||
| 5250 |
dudn1114
|
13년 전 | 1291 | |
| 5249 | 13년 전 | 666 | ||
| 5248 | 13년 전 | 737 | ||
| 5247 | 13년 전 | 2077 | ||
| 5246 | 13년 전 | 501 | ||
| 5245 | 13년 전 | 2190 | ||
| 5244 | 13년 전 | 1450 | ||
| 5243 |
|
13년 전 | 971 | |
| 5242 | 13년 전 | 769 | ||
| 5241 | 13년 전 | 1849 | ||
| 5240 |
|
13년 전 | 505 | |
| 5239 | 13년 전 | 1034 | ||
| 5238 | 13년 전 | 1221 | ||
| 5237 | 13년 전 | 1115 | ||
| 5236 | 13년 전 | 582 | ||
| 5235 |
|
13년 전 | 759 | |
| 5234 |
뭐먹고살지ㅠ
|
13년 전 | 1030 | |
| 5233 | 13년 전 | 3597 | ||
| 5232 | 13년 전 | 3137 | ||
| 5231 | 13년 전 | 3727 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기