<?
//지정된 자릿수의 랜덤한 숫자를 반환합니다. 최대 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년 전
오.. 이런 트윅 너무 좋아합니다. ^^;
고맙습니다. ^^
고맙습니다. ^^
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8030 | 9년 전 | 388 | ||
| 8029 | 9년 전 | 317 | ||
| 8028 | 9년 전 | 275 | ||
| 8027 | 9년 전 | 289 | ||
| 8026 | 9년 전 | 353 | ||
| 8025 | 9년 전 | 395 | ||
| 8024 | 9년 전 | 354 | ||
| 8023 | 9년 전 | 403 | ||
| 8022 | 9년 전 | 326 | ||
| 8021 | 9년 전 | 337 | ||
| 8020 | 9년 전 | 330 | ||
| 8019 | 9년 전 | 349 | ||
| 8018 | 9년 전 | 454 | ||
| 8017 | 9년 전 | 540 | ||
| 8016 | 9년 전 | 342 | ||
| 8015 | 9년 전 | 398 | ||
| 8014 | 9년 전 | 333 | ||
| 8013 | 9년 전 | 246 | ||
| 8012 | 9년 전 | 253 | ||
| 8011 | 9년 전 | 454 | ||
| 8010 | 9년 전 | 309 | ||
| 8009 | 9년 전 | 309 | ||
| 8008 | 9년 전 | 293 | ||
| 8007 | 9년 전 | 437 | ||
| 8006 | 9년 전 | 473 | ||
| 8005 |
|
9년 전 | 976 | |
| 8004 | 9년 전 | 366 | ||
| 8003 | 9년 전 | 432 | ||
| 8002 | 9년 전 | 327 | ||
| 8001 |
|
9년 전 | 671 | |
| 8000 | 9년 전 | 432 | ||
| 7999 | 9년 전 | 383 | ||
| 7998 | 9년 전 | 440 | ||
| 7997 | 9년 전 | 319 | ||
| 7996 | 9년 전 | 544 | ||
| 7995 | 9년 전 | 467 | ||
| 7994 | 9년 전 | 345 | ||
| 7993 | 9년 전 | 409 | ||
| 7992 | 9년 전 | 519 | ||
| 7991 | 9년 전 | 261 | ||
| 7990 | 9년 전 | 302 | ||
| 7989 | 9년 전 | 314 | ||
| 7988 | 9년 전 | 741 | ||
| 7987 | 9년 전 | 439 | ||
| 7986 | 9년 전 | 436 | ||
| 7985 | 9년 전 | 517 | ||
| 7984 | 9년 전 | 439 | ||
| 7983 | 9년 전 | 675 | ||
| 7982 | 9년 전 | 536 | ||
| 7981 | 9년 전 | 489 | ||
| 7980 | 9년 전 | 510 | ||
| 7979 | 9년 전 | 490 | ||
| 7978 | 9년 전 | 467 | ||
| 7977 | 9년 전 | 406 | ||
| 7976 | 9년 전 | 861 | ||
| 7975 | 9년 전 | 374 | ||
| 7974 | 9년 전 | 405 | ||
| 7973 | 9년 전 | 608 | ||
| 7972 | 9년 전 | 394 | ||
| 7971 | 9년 전 | 467 | ||
| 7970 | 9년 전 | 312 | ||
| 7969 | 9년 전 | 553 | ||
| 7968 | 9년 전 | 391 | ||
| 7967 | 9년 전 | 381 | ||
| 7966 | 9년 전 | 383 | ||
| 7965 |
|
9년 전 | 1023 | |
| 7964 | 9년 전 | 411 | ||
| 7963 | 9년 전 | 411 | ||
| 7962 | 9년 전 | 396 | ||
| 7961 |
전갈자리남자
|
9년 전 | 513 | |
| 7960 | 9년 전 | 976 | ||
| 7959 | 9년 전 | 560 | ||
| 7958 | 9년 전 | 419 | ||
| 7957 | 9년 전 | 375 | ||
| 7956 | 9년 전 | 373 | ||
| 7955 | 9년 전 | 468 | ||
| 7954 | 9년 전 | 393 | ||
| 7953 | 9년 전 | 444 | ||
| 7952 | 9년 전 | 365 | ||
| 7951 | 9년 전 | 508 | ||
| 7950 | 9년 전 | 400 | ||
| 7949 | 9년 전 | 396 | ||
| 7948 | 9년 전 | 332 | ||
| 7947 | 9년 전 | 945 | ||
| 7946 | 9년 전 | 449 | ||
| 7945 | 9년 전 | 405 | ||
| 7944 | 9년 전 | 440 | ||
| 7943 | 9년 전 | 394 | ||
| 7942 | 9년 전 | 410 | ||
| 7941 | 9년 전 | 407 | ||
| 7940 | 9년 전 | 900 | ||
| 7939 | 9년 전 | 366 | ||
| 7938 | 9년 전 | 402 | ||
| 7937 | 9년 전 | 291 | ||
| 7936 | 9년 전 | 891 | ||
| 7935 | 9년 전 | 463 | ||
| 7934 | 9년 전 | 432 | ||
| 7933 | 9년 전 | 539 | ||
| 7932 | 9년 전 | 502 | ||
| 7931 | 9년 전 | 553 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기