PHP日常模拟业务的小工具

时间:2021-10-13 19:43:40

随机生成姓名

 public function getChar($num=2)  // $num为生成汉字的数量
{
$first = array('赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨','朱','秦',
'尤','许','何','吕','施','张','孔','曹','严','华','金','魏','陶','姜','戚','谢','邹',
'喻','柏','水','窦','章','云','苏','潘','葛','奚','范','彭','郎','鲁','韦','昌','马',
'苗','凤','花','方','任','袁','柳','鲍','史','唐','费','薛','雷','贺','倪','汤','滕',
'殷','罗','毕','郝','安','常','傅','卞','齐','元','顾','孟','平','黄','穆','萧','尹',
'姚','邵','湛','汪','祁','毛','狄','米','伏','成','戴','谈','宋','茅','庞','熊','纪',
'舒','屈','项','祝','董','梁','杜','阮','蓝','闵','季','贾','路','娄','江','童','颜',
'郭','梅','盛','林','钟','徐','邱','骆','高','夏','蔡','田','樊','胡','凌','霍','虞',
'万','支','柯','管','卢','莫','柯','房','裘','缪','解','应','宗','丁','宣','邓','单',
'杭','洪','包','诸','左','石','崔','吉','龚','程','嵇','邢','裴','陆','荣','翁','荀',
'于','惠','甄','曲','封','储','仲'); $b = '';
for ($i=0; $i<$num; $i++) {
// 使用chr()函数拼接双字节汉字,前一个chr()为高位字节,后一个为低位字节
$a = chr(mt_rand(0xB0,0xD0)).chr(mt_rand(0xA1, 0xF0));
// 转码
$b .= iconv('GB2312', 'UTF-8', $a);
}
$f = array_rand($first);
return $first[$f].$b;
}

随即生成手机号

/**
* @return string
* 手机号获取
*/
public function getMobile()
{
$arr = array(
130,131,132,133,134,135,136,137,138,139,144,147,
150,151,152,153,155,156,157,158,159,
176,177,178,
180,181,182,183,184,185,186,187,188,189,
); $mobile = $arr[array_rand($arr)].mt_rand(1000,9999).mt_rand(1000,9999);
return $mobile;
}

分表计算

/**
* 分表时
* 针对字符串获取数字
*/
public function test11()
{
$str = crc32("471p602pvqpo");
printf("%u\n",$str);
}
function calc_hash_db($u, $s = 4){
$h = sprintf("%u", crc32($u));
$h1 = intval(fmod($h, $s));
return $h1;
}
for($i=1;$i< 100;$i++){
echo calc_hash_db($i);
echo "
";
}
function calc_hash_tbl($u, $n = 256, $m = 16){
$h = sprintf("%u", crc32($u));
$h1 = intval($h / $n);
$h2 = $h1 % $n;
$h3 = base_convert($h2, 10, $m);
$h4 = sprintf("%02s", $h3);
return $h4;
} 注:如果我们不适用%u 可能会得到负数