<?
//지정된 자릿수의 랜덤한 숫자를 반환합니다. 최대 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년 전
오.. 이런 트윅 너무 좋아합니다. ^^;
고맙습니다. ^^
고맙습니다. ^^
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 4730 |
제너레이션
|
13년 전 | 996 | |
| 4729 | 13년 전 | 934 | ||
| 4728 | 13년 전 | 693 | ||
| 4727 | 13년 전 | 1242 | ||
| 4726 | 13년 전 | 844 | ||
| 4725 | 13년 전 | 1678 | ||
| 4724 | 13년 전 | 1013 | ||
| 4723 | 13년 전 | 2686 | ||
| 4722 | 13년 전 | 551 | ||
| 4721 |
ECEditor
|
13년 전 | 2229 | |
| 4720 | 13년 전 | 2606 | ||
| 4719 | 13년 전 | 4369 | ||
| 4718 | 13년 전 | 937 | ||
| 4717 | 13년 전 | 1167 | ||
| 4716 | 13년 전 | 1343 | ||
| 4715 |
한번잘해보자
|
13년 전 | 1162 | |
| 4714 | 13년 전 | 1162 | ||
| 4713 | 13년 전 | 2818 | ||
| 4712 | 13년 전 | 1243 | ||
| 4711 |
김준수사랑
|
13년 전 | 1006 | |
| 4710 |
한번잘해보자
|
13년 전 | 1133 | |
| 4709 | 13년 전 | 1130 | ||
| 4708 | 13년 전 | 806 | ||
| 4707 | 13년 전 | 1696 | ||
| 4706 |
프로프리랜서
|
13년 전 | 2078 | |
| 4705 |
프로프리랜서
|
13년 전 | 1369 | |
| 4704 | 13년 전 | 961 | ||
| 4703 |
프로프리랜서
|
13년 전 | 928 | |
| 4702 | 13년 전 | 822 | ||
| 4701 | 13년 전 | 1565 | ||
| 4700 | 13년 전 | 2587 | ||
| 4699 | 13년 전 | 1292 | ||
| 4698 |
|
13년 전 | 1591 | |
| 4697 |
|
13년 전 | 1111 | |
| 4696 |
|
13년 전 | 1130 | |
| 4695 |
|
13년 전 | 1337 | |
| 4694 |
|
13년 전 | 860 | |
| 4693 |
|
13년 전 | 845 | |
| 4692 |
|
13년 전 | 856 | |
| 4691 |
|
13년 전 | 1185 | |
| 4690 |
|
13년 전 | 1267 | |
| 4689 |
|
13년 전 | 1011 | |
| 4688 |
|
13년 전 | 779 | |
| 4687 |
|
13년 전 | 1055 | |
| 4686 |
|
13년 전 | 1007 | |
| 4685 |
|
13년 전 | 934 | |
| 4684 |
|
13년 전 | 1036 | |
| 4683 |
|
13년 전 | 928 | |
| 4682 |
|
13년 전 | 1239 | |
| 4681 |
|
13년 전 | 1014 | |
| 4680 |
|
13년 전 | 1134 | |
| 4679 |
|
13년 전 | 1645 | |
| 4678 |
|
13년 전 | 576 | |
| 4677 | 13년 전 | 2910 | ||
| 4676 |
복이219
|
13년 전 | 613 | |
| 4675 | 13년 전 | 809 | ||
| 4674 | 13년 전 | 603 | ||
| 4673 | 13년 전 | 983 | ||
| 4672 | 13년 전 | 925 | ||
| 4671 | 13년 전 | 1193 | ||
| 4670 | 13년 전 | 706 | ||
| 4669 | 13년 전 | 1776 | ||
| 4668 | 13년 전 | 1417 | ||
| 4667 |
너는나의봄이다
|
13년 전 | 1014 | |
| 4666 | 13년 전 | 6684 | ||
| 4665 | 13년 전 | 601 | ||
| 4664 | 13년 전 | 986 | ||
| 4663 | 13년 전 | 834 | ||
| 4662 | 13년 전 | 960 | ||
| 4661 | 13년 전 | 1234 | ||
| 4660 | 13년 전 | 831 | ||
| 4659 | 13년 전 | 1094 | ||
| 4658 |
소나무닷컴
|
13년 전 | 684 | |
| 4657 | 13년 전 | 699 | ||
| 4656 |
|
13년 전 | 1229 | |
| 4655 | 13년 전 | 3084 | ||
| 4654 | 13년 전 | 667 | ||
| 4653 | 13년 전 | 2093 | ||
| 4652 | 13년 전 | 864 | ||
| 4651 | 13년 전 | 768 | ||
| 4650 | 13년 전 | 1652 | ||
| 4649 | 13년 전 | 1578 | ||
| 4648 | 13년 전 | 598 | ||
| 4647 | 13년 전 | 1495 | ||
| 4646 | 13년 전 | 1927 | ||
| 4645 | 13년 전 | 1326 | ||
| 4644 | 13년 전 | 586 | ||
| 4643 | 13년 전 | 2001 | ||
| 4642 | 13년 전 | 797 | ||
| 4641 |
|
13년 전 | 583 | |
| 4640 | 13년 전 | 554 | ||
| 4639 |
|
13년 전 | 1287 | |
| 4638 |
|
13년 전 | 1662 | |
| 4637 |
|
13년 전 | 1123 | |
| 4636 | 13년 전 | 612 | ||
| 4635 | 13년 전 | 750 | ||
| 4634 | 13년 전 | 1250 | ||
| 4633 | 13년 전 | 1198 | ||
| 4632 |
|
13년 전 | 534 | |
| 4631 | 13년 전 | 3338 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기