Home  >  Article  >  Backend Development  >  Sharing of code examples for randomly generating names with custom classes in PHP

Sharing of code examples for randomly generating names with custom classes in PHP

黄舟
黄舟Original
2017-08-07 09:28:551266browse

This article mainly introduces PHP's method of randomly generating names based on custom classes, and analyzes related implementation techniques of PHP's random number operations based on arrays and strings to generate names in the form of examples. Friends in need can refer to the following

The example in this article describes how PHP randomly generates names based on custom classes. Share it with everyone for your reference, the details are as follows:

Custom class rndChinaName.class.php:


<?php
Class rndChinaName
{
  private $arrXing,$numbXing;
  private $arrMing,$numbMing;
  function rndChinaName()
  {
    $this->getXingList();
    $this->getMingList();
  }
  /*
  获取姓列表
  */
  private function getXingList()
  {
    $this->arrXing=array(&#39;赵&#39;,&#39;钱&#39;,&#39;孙&#39;,&#39;李&#39;,&#39;周&#39;,&#39;吴&#39;,&#39;郑&#39;,&#39;王&#39;,&#39;冯&#39;,&#39;陈&#39;,&#39;褚&#39;,&#39;卫&#39;,&#39;蒋&#39;,&#39;沈&#39;,&#39;韩&#39;,&#39;杨&#39;,&#39;朱&#39;,&#39;秦&#39;,&#39;尤&#39;,&#39;许&#39;,&#39;何&#39;,&#39;吕&#39;,&#39;施&#39;,&#39;张&#39;,&#39;孔&#39;,&#39;曹&#39;,&#39;严&#39;,&#39;华&#39;,&#39;金&#39;,&#39;魏&#39;,&#39;陶&#39;,&#39;姜&#39;,&#39;戚&#39;,&#39;谢&#39;,&#39;邹&#39;,&#39;喻&#39;,&#39;柏&#39;,&#39;水&#39;,&#39;窦&#39;,&#39;章&#39;,&#39;云&#39;,&#39;苏&#39;,&#39;潘&#39;,&#39;葛&#39;,&#39;奚&#39;,&#39;范&#39;,&#39;彭&#39;,&#39;郎&#39;,&#39;鲁&#39;,&#39;韦&#39;,&#39;昌&#39;,&#39;马&#39;,&#39;苗&#39;,&#39;凤&#39;,&#39;花&#39;,&#39;方&#39;,&#39;任&#39;,&#39;袁&#39;,&#39;柳&#39;,&#39;鲍&#39;,&#39;史&#39;,&#39;唐&#39;,&#39;费&#39;,&#39;薛&#39;,&#39;雷&#39;,&#39;贺&#39;,&#39;倪&#39;,&#39;汤&#39;,&#39;滕&#39;,&#39;殷&#39;,&#39;罗&#39;,&#39;毕&#39;,&#39;郝&#39;,&#39;安&#39;,&#39;常&#39;,&#39;傅&#39;,&#39;卞&#39;,&#39;齐&#39;,&#39;元&#39;,&#39;顾&#39;,&#39;孟&#39;,&#39;平&#39;,&#39;黄&#39;,&#39;穆&#39;,&#39;萧&#39;,&#39;尹&#39;,&#39;姚&#39;,&#39;邵&#39;,&#39;湛&#39;,&#39;汪&#39;,&#39;祁&#39;,&#39;毛&#39;,&#39;狄&#39;,&#39;米&#39;,&#39;伏&#39;,&#39;成&#39;,&#39;戴&#39;,&#39;谈&#39;,&#39;宋&#39;,&#39;茅&#39;,&#39;庞&#39;,&#39;熊&#39;,&#39;纪&#39;,&#39;舒&#39;,&#39;屈&#39;,&#39;项&#39;,&#39;祝&#39;,&#39;董&#39;,&#39;梁&#39;,&#39;杜&#39;,&#39;阮&#39;,&#39;蓝&#39;,&#39;闵&#39;,&#39;季&#39;,&#39;贾&#39;,&#39;路&#39;,&#39;娄&#39;,&#39;江&#39;,&#39;童&#39;,&#39;颜&#39;,&#39;郭&#39;,&#39;梅&#39;,&#39;盛&#39;,&#39;林&#39;,&#39;钟&#39;,&#39;徐&#39;,&#39;邱&#39;,&#39;骆&#39;,&#39;高&#39;,&#39;夏&#39;,&#39;蔡&#39;,&#39;田&#39;,&#39;樊&#39;,&#39;胡&#39;,&#39;凌&#39;,&#39;霍&#39;,&#39;虞&#39;,&#39;万&#39;,&#39;支&#39;,&#39;柯&#39;,&#39;管&#39;,&#39;卢&#39;,&#39;莫&#39;,&#39;柯&#39;,&#39;房&#39;,&#39;裘&#39;,&#39;缪&#39;,&#39;解&#39;,&#39;应&#39;,&#39;宗&#39;,&#39;丁&#39;,&#39;宣&#39;,&#39;邓&#39;,&#39;单&#39;,&#39;杭&#39;,&#39;洪&#39;,&#39;包&#39;,&#39;诸&#39;,&#39;左&#39;,&#39;石&#39;,&#39;崔&#39;,&#39;吉&#39;,&#39;龚&#39;,&#39;程&#39;,&#39;嵇&#39;,&#39;邢&#39;,&#39;裴&#39;,&#39;陆&#39;,&#39;荣&#39;,&#39;翁&#39;,&#39;荀&#39;,&#39;于&#39;,&#39;惠&#39;,&#39;甄&#39;,&#39;曲&#39;,&#39;封&#39;,&#39;储&#39;,&#39;仲&#39;,&#39;伊&#39;,&#39;宁&#39;,&#39;仇&#39;,&#39;甘&#39;,&#39;武&#39;,&#39;符&#39;,&#39;刘&#39;,&#39;景&#39;,&#39;詹&#39;,&#39;龙&#39;,&#39;叶&#39;,&#39;幸&#39;,&#39;司&#39;,&#39;黎&#39;,&#39;溥&#39;,&#39;印&#39;,&#39;怀&#39;,&#39;蒲&#39;,&#39;邰&#39;,&#39;从&#39;,&#39;索&#39;,&#39;赖&#39;,&#39;卓&#39;,&#39;屠&#39;,&#39;池&#39;,&#39;乔&#39;,&#39;胥&#39;,&#39;闻&#39;,&#39;莘&#39;,&#39;党&#39;,&#39;翟&#39;,&#39;谭&#39;,&#39;贡&#39;,&#39;劳&#39;,&#39;逄&#39;,&#39;姬&#39;,&#39;申&#39;,&#39;扶&#39;,&#39;堵&#39;,&#39;冉&#39;,&#39;宰&#39;,&#39;雍&#39;,&#39;桑&#39;,&#39;寿&#39;,&#39;通&#39;,&#39;燕&#39;,&#39;浦&#39;,&#39;尚&#39;,&#39;农&#39;,&#39;温&#39;,&#39;别&#39;,&#39;庄&#39;,&#39;晏&#39;,&#39;柴&#39;,&#39;瞿&#39;,&#39;阎&#39;,&#39;连&#39;,&#39;习&#39;,&#39;容&#39;,&#39;向&#39;,&#39;古&#39;,&#39;易&#39;,&#39;廖&#39;,&#39;庾&#39;,&#39;终&#39;,&#39;步&#39;,&#39;都&#39;,&#39;耿&#39;,&#39;满&#39;,&#39;弘&#39;,&#39;匡&#39;,&#39;国&#39;,&#39;文&#39;,&#39;寇&#39;,&#39;广&#39;,&#39;禄&#39;,&#39;阙&#39;,&#39;东&#39;,&#39;欧&#39;,&#39;利&#39;,&#39;师&#39;,&#39;巩&#39;,&#39;聂&#39;,&#39;关&#39;,&#39;荆&#39;,&#39;司马&#39;,&#39;上官&#39;,&#39;欧阳&#39;,&#39;夏侯&#39;,&#39;诸葛&#39;,&#39;闻人&#39;,&#39;东方&#39;,&#39;赫连&#39;,&#39;皇甫&#39;,&#39;尉迟&#39;,&#39;公羊&#39;,&#39;澹台&#39;,&#39;公冶&#39;,&#39;宗政&#39;,&#39;濮阳&#39;,&#39;淳于&#39;,&#39;单于&#39;,&#39;太叔&#39;,&#39;申屠&#39;,&#39;公孙&#39;,&#39;仲孙&#39;,&#39;轩辕&#39;,&#39;令狐&#39;,&#39;徐离&#39;,&#39;宇文&#39;,&#39;长孙&#39;,&#39;慕容&#39;,&#39;司徒&#39;,&#39;司空&#39;);
    $this->numbXing = count($this-> arrXing); //姓总数
  }
  /*
  获取名列表
  */
  private function getMingList()
  {
    $this->arrMing=array(&#39;伟&#39;,&#39;刚&#39;,&#39;勇&#39;,&#39;毅&#39;,&#39;俊&#39;,&#39;峰&#39;,&#39;强&#39;,&#39;军&#39;,&#39;平&#39;,&#39;保&#39;,&#39;东&#39;,&#39;文&#39;,&#39;辉&#39;,&#39;力&#39;,&#39;明&#39;,&#39;永&#39;,&#39;健&#39;,&#39;世&#39;,&#39;广&#39;,&#39;志&#39;,&#39;义&#39;,&#39;兴&#39;,&#39;良&#39;,&#39;海&#39;,&#39;山&#39;,&#39;仁&#39;,&#39;波&#39;,&#39;宁&#39;,&#39;贵&#39;,&#39;福&#39;,&#39;生&#39;,&#39;龙&#39;,&#39;元&#39;,&#39;全&#39;,&#39;国&#39;,&#39;胜&#39;,&#39;学&#39;,&#39;祥&#39;,&#39;才&#39;,&#39;发&#39;,&#39;武&#39;,&#39;新&#39;,&#39;利&#39;,&#39;清&#39;,&#39;飞&#39;,&#39;彬&#39;,&#39;富&#39;,&#39;顺&#39;,&#39;信&#39;,&#39;子&#39;,&#39;杰&#39;,&#39;涛&#39;,&#39;昌&#39;,&#39;成&#39;,&#39;康&#39;,&#39;星&#39;,&#39;光&#39;,&#39;天&#39;,&#39;达&#39;,&#39;安&#39;,&#39;岩&#39;,&#39;中&#39;,&#39;茂&#39;,&#39;进&#39;,&#39;林&#39;,&#39;有&#39;,&#39;坚&#39;,&#39;和&#39;,&#39;彪&#39;,&#39;博&#39;,&#39;诚&#39;,&#39;先&#39;,&#39;敬&#39;,&#39;震&#39;,&#39;振&#39;,&#39;壮&#39;,&#39;会&#39;,&#39;思&#39;,&#39;群&#39;,&#39;豪&#39;,&#39;心&#39;,&#39;邦&#39;,&#39;承&#39;,&#39;乐&#39;,&#39;绍&#39;,&#39;功&#39;,&#39;松&#39;,&#39;善&#39;,&#39;厚&#39;,&#39;庆&#39;,&#39;磊&#39;,&#39;民&#39;,&#39;友&#39;,&#39;裕&#39;,&#39;河&#39;,&#39;哲&#39;,&#39;江&#39;,&#39;超&#39;,&#39;浩&#39;,&#39;亮&#39;,&#39;政&#39;,&#39;谦&#39;,&#39;亨&#39;,&#39;奇&#39;,&#39;固&#39;,&#39;之&#39;,&#39;轮&#39;,&#39;翰&#39;,&#39;朗&#39;,&#39;伯&#39;,&#39;宏&#39;,&#39;言&#39;,&#39;若&#39;,&#39;鸣&#39;,&#39;朋&#39;,&#39;斌&#39;,&#39;梁&#39;,&#39;栋&#39;,&#39;维&#39;,&#39;启&#39;,&#39;克&#39;,&#39;伦&#39;,&#39;翔&#39;,&#39;旭&#39;,&#39;鹏&#39;,&#39;泽&#39;,&#39;晨&#39;,&#39;辰&#39;,&#39;士&#39;,&#39;以&#39;,&#39;建&#39;,&#39;家&#39;,&#39;致&#39;,&#39;树&#39;,&#39;炎&#39;,&#39;德&#39;,&#39;行&#39;,&#39;时&#39;,&#39;泰&#39;,&#39;盛&#39;,&#39;雄&#39;,&#39;琛&#39;,&#39;钧&#39;,&#39;冠&#39;,&#39;策&#39;,&#39;腾&#39;,&#39;楠&#39;,&#39;榕&#39;,&#39;风&#39;,&#39;航&#39;,&#39;弘&#39;,&#39;秀&#39;,&#39;娟&#39;,&#39;英&#39;,&#39;华&#39;,&#39;慧&#39;,&#39;巧&#39;,&#39;美&#39;,&#39;娜&#39;,&#39;静&#39;,&#39;淑&#39;,&#39;惠&#39;,&#39;珠&#39;,&#39;翠&#39;,&#39;雅&#39;,&#39;芝&#39;,&#39;玉&#39;,&#39;萍&#39;,&#39;红&#39;,&#39;娥&#39;,&#39;玲&#39;,&#39;芬&#39;,&#39;芳&#39;,&#39;燕&#39;,&#39;彩&#39;,&#39;春&#39;,&#39;菊&#39;,&#39;兰&#39;,&#39;凤&#39;,&#39;洁&#39;,&#39;梅&#39;,&#39;琳&#39;,&#39;素&#39;,&#39;云&#39;,&#39;莲&#39;,&#39;真&#39;,&#39;环&#39;,&#39;雪&#39;,&#39;荣&#39;,&#39;爱&#39;,&#39;妹&#39;,&#39;霞&#39;,&#39;香&#39;,&#39;月&#39;,&#39;莺&#39;,&#39;媛&#39;,&#39;艳&#39;,&#39;瑞&#39;,&#39;凡&#39;,&#39;佳&#39;,&#39;嘉&#39;,&#39;琼&#39;,&#39;勤&#39;,&#39;珍&#39;,&#39;贞&#39;,&#39;莉&#39;,&#39;桂&#39;,&#39;娣&#39;,&#39;叶&#39;,&#39;璧&#39;,&#39;璐&#39;,&#39;娅&#39;,&#39;琦&#39;,&#39;晶&#39;,&#39;妍&#39;,&#39;茜&#39;,&#39;秋&#39;,&#39;珊&#39;,&#39;莎&#39;,&#39;锦&#39;,&#39;黛&#39;,&#39;青&#39;,&#39;倩&#39;,&#39;婷&#39;,&#39;姣&#39;,&#39;婉&#39;,&#39;娴&#39;,&#39;瑾&#39;,&#39;颖&#39;,&#39;露&#39;,&#39;瑶&#39;,&#39;怡&#39;,&#39;婵&#39;,&#39;雁&#39;,&#39;蓓&#39;,&#39;纨&#39;,&#39;仪&#39;,&#39;荷&#39;,&#39;丹&#39;,&#39;蓉&#39;,&#39;眉&#39;,&#39;君&#39;,&#39;琴&#39;,&#39;蕊&#39;,&#39;薇&#39;,&#39;菁&#39;,&#39;梦&#39;,&#39;岚&#39;,&#39;苑&#39;,&#39;婕&#39;,&#39;馨&#39;,&#39;瑗&#39;,&#39;琰&#39;,&#39;韵&#39;,&#39;融&#39;,&#39;园&#39;,&#39;艺&#39;,&#39;咏&#39;,&#39;卿&#39;,&#39;聪&#39;,&#39;澜&#39;,&#39;纯&#39;,&#39;毓&#39;,&#39;悦&#39;,&#39;昭&#39;,&#39;冰&#39;,&#39;爽&#39;,&#39;琬&#39;,&#39;茗&#39;,&#39;羽&#39;,&#39;希&#39;,&#39;欣&#39;,&#39;飘&#39;,&#39;育&#39;,&#39;滢&#39;,&#39;馥&#39;,&#39;筠&#39;,&#39;柔&#39;,&#39;竹&#39;,&#39;霭&#39;,&#39;凝&#39;,&#39;晓&#39;,&#39;欢&#39;,&#39;霄&#39;,&#39;枫&#39;,&#39;芸&#39;,&#39;菲&#39;,&#39;寒&#39;,&#39;伊&#39;,&#39;亚&#39;,&#39;宜&#39;,&#39;可&#39;,&#39;姬&#39;,&#39;舒&#39;,&#39;影&#39;,&#39;荔&#39;,&#39;枝&#39;,&#39;丽&#39;,&#39;阳&#39;,&#39;妮&#39;,&#39;宝&#39;,&#39;贝&#39;,&#39;初&#39;,&#39;程&#39;,&#39;梵&#39;,&#39;罡&#39;,&#39;恒&#39;,&#39;鸿&#39;,&#39;桦&#39;,&#39;骅&#39;,&#39;剑&#39;,&#39;娇&#39;,&#39;纪&#39;,&#39;宽&#39;,&#39;苛&#39;,&#39;灵&#39;,&#39;玛&#39;,&#39;媚&#39;,&#39;琪&#39;,&#39;晴&#39;,&#39;容&#39;,&#39;睿&#39;,&#39;烁&#39;,&#39;堂&#39;,&#39;唯&#39;,&#39;威&#39;,&#39;韦&#39;,&#39;雯&#39;,&#39;苇&#39;,&#39;萱&#39;,&#39;阅&#39;,&#39;彦&#39;,&#39;宇&#39;,&#39;雨&#39;,&#39;洋&#39;,&#39;忠&#39;,&#39;宗&#39;,&#39;曼&#39;,&#39;紫&#39;,&#39;逸&#39;,&#39;贤&#39;,&#39;蝶&#39;,&#39;菡&#39;,&#39;绿&#39;,&#39;蓝&#39;,&#39;儿&#39;,&#39;翠&#39;,&#39;烟&#39;);
    $this->numbMing = count($this-> arrMing); //名总数
  }
  private function getXing()
  {
    return $this->arrXing [mt_rand(0,$this->numbXing)];
  }
  private function getMing()
  {
    return $this->arrMing [mt_rand(0,$this->numbMing)];
  }
  public function getName($type=0)
  {
    $name = &#39;&#39; ;
    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;
  }
}
?>

index.php:


<?php
require("rndChinaName.class.php" );
$name=new rndChinaName();
echo $name->getName();
//输出:林艺菁
?>

The above is the detailed content of Sharing of code examples for randomly generating names with custom classes in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn