PHP手机和身份证号打码,生成随机数,判断逗号隔开的数字串等

639人浏览   2024-03-22 21:57:24

/*1、判断字符串是否是逗号隔开的数字*/

function checkNumbericStrByComma($string = "") {
	$pattern = "/^(\d+,)*(\d+)$/";
	$ret = preg_match($pattern, $string);
	return $ret;
}

/*2、手机号打码中间四位用*表示*/

function codeMobile($mobile = "") {
	return preg_replace('/(^.*)\d{4}(\d{4})$/', '\\1****\\2', $mobile);
}

/*3、身份证号打码中间9-14位用*表示*/

function codeIdcard($idcard = "") {
	return strlen($idcard) == 15 ? substr_replace($idcard, "******", 8, 6) : (strlen($idcard) == 18 ? substr_replace($idcard, "******", 8, 6) : "");
}

/**

* 4、生成随机数字符串

* @param string $type 类型
alpha/alnum/numeric/nozero/unique/md5/encrypt/sha1

* @param int $len 长度

* @return string

*/

function randomString($type = 'alnum', $len = 8) {
	switch ($type) {
	case 'alpha':
	case 'alnum':
	case 'numeric':
	case 'nozero':
		switch ($type) {
		case 'alpha':
			$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
		case 'alnum':
			$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			break;
		case 'numeric':
			$pool = '0123456789';
			break;
		case 'nozero':
			$pool = '123456789';
			break;
		}
		return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
	case 'unique':
	case 'md5':
		return md5(uniqid(mt_rand()));
	case 'encrypt':
	case 'sha1':
		return sha1(uniqid(mt_rand(), TRUE));
	}
}