<?
//지정된 자릿수의 랜덤한 숫자를 반환합니다. 최대 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년 전
오.. 이런 트윅 너무 좋아합니다. ^^;
고맙습니다. ^^
고맙습니다. ^^
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 5230 | 13년 전 | 3071 | ||
| 5229 | 13년 전 | 576 | ||
| 5228 | 13년 전 | 621 | ||
| 5227 | 13년 전 | 725 | ||
| 5226 | 13년 전 | 9181 | ||
| 5225 | 13년 전 | 2191 | ||
| 5224 | 13년 전 | 2605 | ||
| 5223 | 13년 전 | 2580 | ||
| 5222 | 13년 전 | 3146 | ||
| 5221 |
visualp
|
13년 전 | 725 | |
| 5220 |
|
13년 전 | 772 | |
| 5219 | 13년 전 | 4665 | ||
| 5218 |
|
13년 전 | 701 | |
| 5217 |
크라이스트
|
13년 전 | 1382 | |
| 5216 | 13년 전 | 1245 | ||
| 5215 | 13년 전 | 1260 | ||
| 5214 | 13년 전 | 1133 | ||
| 5213 | 13년 전 | 688 | ||
| 5212 | 13년 전 | 1367 | ||
| 5211 | 13년 전 | 758 | ||
| 5210 |
visualp
|
13년 전 | 1484 | |
| 5209 |
visualp
|
13년 전 | 1099 | |
| 5208 |
mammoth
|
13년 전 | 737 | |
| 5207 | 13년 전 | 2115 | ||
| 5206 | 13년 전 | 1090 | ||
| 5205 | 13년 전 | 699 | ||
| 5204 |
|
13년 전 | 2267 | |
| 5203 | 13년 전 | 1187 | ||
| 5202 | 13년 전 | 1214 | ||
| 5201 |
kastel
|
13년 전 | 1374 | |
| 5200 | 13년 전 | 1087 | ||
| 5199 | 13년 전 | 571 | ||
| 5198 | 13년 전 | 635 | ||
| 5197 | 13년 전 | 1571 | ||
| 5196 | 13년 전 | 788 | ||
| 5195 | 13년 전 | 1799 | ||
| 5194 | 13년 전 | 1300 | ||
| 5193 | 13년 전 | 617 | ||
| 5192 | 13년 전 | 4065 | ||
| 5191 | 13년 전 | 745 | ||
| 5190 |
|
13년 전 | 1862 | |
| 5189 | 13년 전 | 1199 | ||
| 5188 | 13년 전 | 1549 | ||
| 5187 |
믹스디자인
|
13년 전 | 817 | |
| 5186 |
|
13년 전 | 2391 | |
| 5185 | 13년 전 | 1434 | ||
| 5184 | 13년 전 | 897 | ||
| 5183 | 13년 전 | 1406 | ||
| 5182 | 13년 전 | 683 | ||
| 5181 | 13년 전 | 717 | ||
| 5180 |
프리온관리자
|
13년 전 | 717 | |
| 5179 | 13년 전 | 987 | ||
| 5178 | 13년 전 | 650 | ||
| 5177 | 13년 전 | 575 | ||
| 5176 | 13년 전 | 693 | ||
| 5175 | 13년 전 | 1427 | ||
| 5174 | 13년 전 | 872 | ||
| 5173 | 13년 전 | 603 | ||
| 5172 |
|
13년 전 | 576 | |
| 5171 | 13년 전 | 911 | ||
| 5170 |
KhanSu
|
13년 전 | 705 | |
| 5169 | 13년 전 | 2166 | ||
| 5168 | 13년 전 | 1491 | ||
| 5167 |
sir24
|
13년 전 | 686 | |
| 5166 | 13년 전 | 1753 | ||
| 5165 | 13년 전 | 734 | ||
| 5164 | 13년 전 | 1660 | ||
| 5163 |
|
13년 전 | 997 | |
| 5162 |
곰도리푸우
|
13년 전 | 4094 | |
| 5161 |
UltraUCC
|
13년 전 | 1231 | |
| 5160 |
|
13년 전 | 886 | |
| 5159 |
그런듯아닌듯
|
13년 전 | 545 | |
| 5158 |
|
13년 전 | 521 | |
| 5157 | 13년 전 | 1657 | ||
| 5156 | 13년 전 | 1161 | ||
| 5155 | 13년 전 | 2652 | ||
| 5154 | 13년 전 | 797 | ||
| 5153 | 13년 전 | 790 | ||
| 5152 |
|
13년 전 | 629 | |
| 5151 | 13년 전 | 836 | ||
| 5150 |
senseme
|
13년 전 | 560 | |
| 5149 |
워드앤코드
|
13년 전 | 1011 | |
| 5148 | 13년 전 | 706 | ||
| 5147 |
프로프리랜서
|
13년 전 | 908 | |
| 5146 | 13년 전 | 2403 | ||
| 5145 |
투코아미디어
|
13년 전 | 1505 | |
| 5144 |
there007
|
13년 전 | 1512 | |
| 5143 | 13년 전 | 1375 | ||
| 5142 | 13년 전 | 2095 | ||
| 5141 |
silky
|
13년 전 | 767 | |
| 5140 |
silky
|
13년 전 | 645 | |
| 5139 |
silky
|
13년 전 | 1985 | |
| 5138 |
silky
|
13년 전 | 586 | |
| 5137 |
silky
|
13년 전 | 614 | |
| 5136 |
silky
|
13년 전 | 898 | |
| 5135 |
silky
|
13년 전 | 623 | |
| 5134 |
silky
|
13년 전 | 477 | |
| 5133 |
|
13년 전 | 846 | |
| 5132 |
복이219
|
13년 전 | 973 | |
| 5131 |
바부바부팅이
|
13년 전 | 1019 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기