<?
//지정된 자릿수의 랜덤한 숫자를 반환합니다. 최대 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년 전
오.. 이런 트윅 너무 좋아합니다. ^^;
고맙습니다. ^^
고맙습니다. ^^
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 330 |
prosper
|
20년 전 | 2264 | |
| 329 |
prosper
|
20년 전 | 1886 | |
| 328 |
prosper
|
20년 전 | 1690 | |
| 327 | 20년 전 | 3757 | ||
| 326 | 20년 전 | 4850 | ||
| 325 |
hwatta
|
20년 전 | 2470 | |
| 324 |
|
20년 전 | 3138 | |
| 323 | 20년 전 | 5853 | ||
| 322 |
hwatta
|
20년 전 | 2840 | |
| 321 |
hwatta
|
20년 전 | 2313 | |
| 320 |
yesmoa
|
20년 전 | 4568 | |
| 319 | 20년 전 | 2679 | ||
| 318 | 20년 전 | 2235 | ||
| 317 |
kyodon
|
20년 전 | 2759 | |
| 316 | 20년 전 | 2574 | ||
| 315 |
|
20년 전 | 2879 | |
| 314 |
|
20년 전 | 3336 | |
| 313 |
|
20년 전 | 2639 | |
| 312 |
yesmoa
|
20년 전 | 4710 | |
| 311 | 20년 전 | 3264 | ||
| 310 |
홀로남은자
|
20년 전 | 4558 | |
| 309 | 20년 전 | 3004 | ||
| 308 | 20년 전 | 4126 | ||
| 307 | 20년 전 | 4369 | ||
| 306 | 20년 전 | 7032 | ||
| 305 | 20년 전 | 3867 | ||
| 304 | 20년 전 | 2762 | ||
| 303 |
크리스탈처럼
|
20년 전 | 4395 | |
| 302 | 20년 전 | 2202 | ||
| 301 |
|
20년 전 | 4318 | |
| 300 | 20년 전 | 3802 | ||
| 299 | 20년 전 | 2609 | ||
| 298 | 20년 전 | 4822 | ||
| 297 |
|
20년 전 | 2524 | |
| 296 | 20년 전 | 4522 | ||
| 295 | 20년 전 | 3565 | ||
| 294 | 20년 전 | 3593 | ||
| 293 | 20년 전 | 3815 | ||
| 292 | 20년 전 | 3205 | ||
| 291 |
yesmoa
|
20년 전 | 5936 | |
| 290 | 20년 전 | 2935 | ||
| 289 | 20년 전 | 5849 | ||
| 288 |
|
20년 전 | 2374 | |
| 287 |
|
20년 전 | 1795 | |
| 286 |
|
20년 전 | 2146 | |
| 285 |
|
20년 전 | 3532 | |
| 284 |
|
20년 전 | 2030 | |
| 283 |
|
20년 전 | 4400 | |
| 282 | 20년 전 | 3381 | ||
| 281 |
|
20년 전 | 2211 | |
| 280 |
|
20년 전 | 7804 | |
| 279 | 20년 전 | 5553 | ||
| 278 | 20년 전 | 2990 | ||
| 277 |
|
20년 전 | 5569 | |
| 276 | 20년 전 | 2355 | ||
| 275 | 20년 전 | 2602 | ||
| 274 | 20년 전 | 2380 | ||
| 273 | 20년 전 | 2217 | ||
| 272 | 20년 전 | 2149 | ||
| 271 | 20년 전 | 2600 | ||
| 270 | 20년 전 | 2632 | ||
| 269 | 20년 전 | 2482 | ||
| 268 | 20년 전 | 2683 | ||
| 267 | 20년 전 | 2365 | ||
| 266 | 20년 전 | 2562 | ||
| 265 | 20년 전 | 3505 | ||
| 264 |
|
20년 전 | 5358 | |
| 263 |
|
20년 전 | 3728 | |
| 262 | 20년 전 | 3180 | ||
| 261 |
허저비
|
20년 전 | 5927 | |
| 260 |
|
20년 전 | 5715 | |
| 259 | 20년 전 | 4138 | ||
| 258 | 20년 전 | 2376 | ||
| 257 | 20년 전 | 3192 | ||
| 256 | 20년 전 | 1901 | ||
| 255 | 20년 전 | 1572 | ||
| 254 | 20년 전 | 3142 | ||
| 253 | 20년 전 | 3545 | ||
| 252 | 20년 전 | 5122 | ||
| 251 | 20년 전 | 5808 | ||
| 250 | 20년 전 | 3675 | ||
| 249 | 20년 전 | 5014 | ||
| 248 | 20년 전 | 3286 | ||
| 247 | 20년 전 | 3640 | ||
| 246 |
|
20년 전 | 7954 | |
| 245 |
|
20년 전 | 5909 | |
| 244 | 20년 전 | 4487 | ||
| 243 |
|
20년 전 | 4054 | |
| 242 | 20년 전 | 2783 | ||
| 241 | 20년 전 | 2730 | ||
| 240 | 20년 전 | 2367 | ||
| 239 | 20년 전 | 1662 | ||
| 238 |
아우겐나이스
|
20년 전 | 2267 | |
| 237 |
email
|
20년 전 | 3688 | |
| 236 | 20년 전 | 4156 | ||
| 235 | 20년 전 | 10462 | ||
| 234 | 20년 전 | 5071 | ||
| 233 | 20년 전 | 3371 | ||
| 232 | 20년 전 | 3211 | ||
| 231 | 20년 전 | 3842 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기