PHP隨機中國人姓名的類

老規矩,直接上代碼:

<?php

/*rndChinaName.class.php*/
Class RandChinaName
{
    private $arrXing,$numbXing;
    private $arrMing,$numbMing;

    function __construct()
    {
        $this->getXingList();
        $this->getMingList();
    }

    /* 獲取姓列表 */
    private function getXingList()
    {

        $this->arrXing=array(
            '','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','滿','','','','',
            '','','祿','','','','','','','','','','司馬','上官','歐陽','夏侯',
            '諸葛','聞人','東方','赫連','皇甫','尉遲','公羊','澹臺','公冶','宗政','濮陽','淳于','單于',
            '太叔','申屠','公孫','仲孫','軒轅','令狐','徐離','宇文','長孫','慕容','司徒','司空');

        $this->numbXing = count($this->arrXing); //姓總數

    }


    /* 獲取名列表 */
    private function getMingList()
    {
        $this->arrMing=array(
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','','','','','','','','',
            '','','','','','','','','','','','','','');

        //名總數
        $this->numbMing = count($this->arrMing);
    }


    // 獲取姓
    private function getXing()
    {
        // mt_rand() 比rand()方法快四倍,而且生成的隨機數比rand()生成的僞隨機數無規律。
        return $this->arrXing[mt_rand(0,$this->numbXing-1)];

    }

    // 獲取名字
    private function getMing()
    {
        return $this->arrMing[mt_rand(0,$this->numbMing-1)];
    }


    // 獲取名字
    public function getName($type=0)
    {
        $name = '' ;
        switch($type)
        {
            case 1:    //2字
                $name = $this->getXing().$this->getMing();
                break;
            case 2:    //隨機2、3個字
                $name = $this->getXing().$this->getMing();
                if(mt_rand(0,100)>50)$name .= $this->getMing();
                break;
            case 3: //只取姓
                $name = $this->getXing();
                break;
            case 4: //只取名
                $name = $this->getMing();
                break;
            case 0:
            default: //默認情況 1姓+2名
                $name = $this->getXing().$this->getMing().$this->getMing();


        }

        return $name;
    }

}

如何使用:

 $obj= new RandChinaName();
 $name =  $obj->getName();

運行結果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章