PHP隨機生成用戶信息實例分析

本文給大傢介紹如何隨機生成用戶信息(號碼、名字、時間),希望對需要的朋友有所幫助!

隨機生成號碼

function generate_name($count,$type="array",$white_space=false)
{
$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,
);
// for($i = 0; $i < $count; $i++) {
// $tmp[] = $arr[array_rand($arr)].' '.mt_rand(1000,9999).' '.mt_rand(1000,9999);
// }
$tmp = $arr[array_rand($arr)].' '.mt_rand(1000,9999).' '.mt_rand(1000,9999);
if($type==="string"){
$tmp=json_encode($tmp);//如果是字符串,解析成字符串
}
if($white_space===true){
$tmp=preg_replace("/\s*/","",$tmp);
}
return $tmp;
}

隨機生成姓名

function generate_name($count,$type="array",$white_space=false)
{
 
    $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,
    );
 
    // for($i = 0; $i < $count; $i++) {
 
    //     $tmp[] = $arr[array_rand($arr)].' '.mt_rand(1000,9999).' '.mt_rand(1000,9999);
 
    // }
    $tmp = $arr[array_rand($arr)].' '.mt_rand(1000,9999).' '.mt_rand(1000,9999);
    if($type==="string"){
        $tmp=json_encode($tmp);//如果是字符串,解析成字符串
    }
    if($white_space===true){
        $tmp=preg_replace("/\s*/","",$tmp);
    }
    return $tmp;
}
//隨機生成用戶名
public function generateName(){
    $arrXing = $this->getXingList();
    $numbXing = count($arrXing);
    $arrMing = $this->getMingList();
    $numbMing =  count($arrMing);
    $Xing = $arrXing[mt_rand(0,$numbXing-1)];
    $Ming = $arrMing[mt_rand(0,$numbMing-1)].$arrMing[mt_rand(0,$numbMing-1)];
    $name = $Xing.$Ming;
    return $name;
}
 
//獲取姓氏
 
public function getXingList(){
    $arrXing=array('趙','錢','孫','李','周','吳','鄭'
    , '王','馮','陳','褚','衛','蔣','沈'
    ,'韓','楊','朱','秦','尤','許','何','呂','施','張'
    ,'孔','曹','嚴','華','金','魏','陶','薑','戚','謝','鄒',
        '喻','柏','水','竇','章','雲','蘇','潘','葛'
    ,'奚','范','彭','郎','魯','韋','昌'
    ,'馬','苗','鳳','花','方','任','袁','柳','鮑','史'
    ,'唐','費','薛','雷','賀','倪','湯','滕','殷','羅'
    , '畢','郝','安','常','傅','卞','齊','元','顧','孟'
    ,'平','黃','穆','蕭','尹','姚'
    ,'邵','湛','汪','祁','毛','狄','米','伏','成','戴'
    ,'談','宋','茅','龐','熊','紀','舒','屈','項','祝',
        '董','梁','杜','阮','藍','閔','季','賈','路'
    ,'婁','江','童','顏','郭','梅','盛'
    ,'林','鐘','徐','邱','駱','高','夏','蔡','田','樊'
    ,'胡','凌','霍','虞','萬','支','柯','管','盧','莫',
        '柯','房','裘','繆','解','應','宗','丁','宣','鄧'
    ,'單','杭','洪','包','諸','左'
    ,'石','崔','吉','龔','程','嵇','邢','裴','陸','榮'
    ,'翁','荀','於','惠','甄','曲','封','儲','仲','伊',
        '寧','仇','甘','武','符','劉','景','詹','龍'
    ,'葉','幸','司','黎','溥','印','懷'
    ,'蒲','邰','從','索','賴','卓','屠','池','喬','胥'
    ,'聞','莘','黨','翟','譚','貢','勞','逄','姬','申',
        '扶','堵','冉','宰','雍','桑','壽','通','燕','浦'
    ,'尚','農','溫','別','莊','晏'
    ,'柴','瞿','閻','連','習','容','向','古','易','廖'
    ,'庾','終','步','都','耿','滿','弘','匡','國','文',
        '寇','廣','祿','闕','東','歐','利','師','鞏'
    ,'聶','關','荊','司馬','上官','歐陽'
    ,'夏侯','諸葛','聞人','東方','赫連','皇甫','尉遲'
    ,'公羊','澹臺','公冶','宗政','濮陽','淳於','單於','太叔',
        '申屠','公孫','仲孫','軒轅','令狐','徐離'
    ,'宇文','長孫','慕容','司徒','司空');
    return $arrXing;
}
//獲取名字
public function getMingList(){
    $arrMing=array(
        '偉','剛','勇','毅','俊','峰','強','軍','平'
    ,'保','東','文','輝','力','明','永', '健',
        '世','廣','志','義','興','良','海','山','仁'
    ,'波','寧','貴','福','生','龍','元','全'
    ,'國','勝','學','祥','才','發','武','新','利','清'
    ,'飛','彬','富','順','信','子','傑','濤',
        '昌','成','康','星','光','天','達','安','巖'
    ,'中','茂','進','林','有','堅','和','彪','博','誠'
    ,'先','敬','震','振','壯','會','思','群','豪','心'
    ,'邦','承','樂','紹','功','松','善','厚',
        '慶','磊','民','友','裕','河','哲','江','超'
    ,'浩','亮','政','謙','亨','奇','固','之','輪','翰'
    ,'朗','伯','宏','言','若','鳴','朋','斌','梁','棟'
    ,'維','啟','克','倫','翔','旭','鵬','澤',
        '晨','辰','士','以','建','傢','致','樹','炎'
    ,'德','行','時','泰','盛','雄','琛','鈞','冠','策'
    ,'騰','楠','榕','風','航','弘','秀','娟','英','華'
    ,'慧','巧','美','娜','靜','淑','惠','珠',
        '翠','雅','芝','玉','萍','紅','娥','玲','芬'
    ,'芳','燕','彩','春','菊','蘭','鳳','潔','梅','琳'
    ,'素','雲','蓮','真','環','雪','榮','愛','妹','霞'
    ,'香','月','鶯','媛','艷','瑞','凡','佳',
        '嘉','瓊','勤','珍','貞','莉','桂','娣','葉'
    ,'璧','璐','婭','琦','晶','妍','茜','秋','珊', '莎'
    ,'錦','黛','青','倩','婷','姣','婉','嫻','瑾','穎'
    ,'露','瑤','怡','嬋','雁','蓓','紈','儀','荷'
    ,'丹','蓉','眉','君','琴','蕊','薇','菁','夢','嵐'
    ,'苑','婕','馨','瑗','琰','韻','融','園'
    ,'藝','詠','卿','聰','瀾','純','毓','悅','昭','冰'
    ,'爽','琬','茗','羽','希','欣','飄','育','瀅'
    ,'馥','筠','柔','竹','靄','凝','曉','歡','霄','楓'
    ,'蕓','菲','寒','伊','亞','宜','可','姬'
    ,'舒','影','荔','枝','麗','陽','妮','寶','貝','初'
    ,'程','梵','罡','恒','鴻','樺','驊','劍','嬌'
    ,'紀','寬','苛','靈','瑪','媚','琪','晴','容','睿'
    ,'爍','堂','唯','威','韋','雯','葦','萱'
    ,'閱','彥','宇','雨','洋','忠','宗','曼','紫','逸'
    ,'賢','蝶','菡','綠','藍','兒','翠','煙');
    return $arrMing;
}

實例擴展:

php隨機生成昵稱,PHP生成隨機用戶名

public static function nicknames($data) {
if ($data['request'] == 'nickAvailable') {
foreach ($data as $value)
if (is_array($value))
$nick = $value['nickname'];
$random = rand(2, 2);
$nickname = $nick . '_' . $random;
$count = 3;
$nicknames = array();
for ($i = 1; $i <= $count; $i++) {
$select = self::$db->select('users', 'nickname', array('nickname' => $nickname));
if (count($select) == 0) {
$nicknames[] = $nickname;
} else {
$count = $count + 1;
}
}
$array = array("status" => 0,
"errors" => $nicknames,
"data" => array());
model::json($array);
}
}

到此這篇關於PHP隨機生成用戶信息實例分析的文章就介紹到這瞭,更多相關PHP怎麼隨機生成用戶信息內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: