|Backend.DevLog/PHP

randomstring

케리's 2022. 5. 18. 17:58

1) 스크립트

<?php
$couponno = get_coupon("cc",15);
print_r($couponno);



function get_coupon($type,$length=16){
	// num은 숫자만, char은 문자만, 기본값은 영,숫자혼용
	switch($type){
		case 'NUM':
			$characters = '0123456789';
		break;
		case 'CHAR':
			$characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		break;
		default:
			$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	}


	$charactersLength = strlen($characters);
	$randomString = '';
	for ($i = 0; $i < $length; $i++) {
		$randomString .= $characters[rand(0, $charactersLength - 1)];
	}
	return $randomString;

}

2) 실행 

(base) ila@baghyeliui-MacBookPro test % php test.php
RKSJGB4WAKONLVC%