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();

运行结果:

 

posted @ 2020-12-11 10:27  山上小和尚  阅读(195)  评论(0编辑  收藏  举报