


Detailed explanation of PHP's class for randomly generating Chinese names
PHP randomly generates Chinese name class
PHP randomly generates class
<?php /*rndChinaName.class.php*/ Class rndChinaName { private $arrXing,$numbXing; private $arrMing,$numbMing; function rndChinaName() { $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; } }
Use Method:
<?php // index.php header('Content-type: text/html; charset=utf-8'); // 引入文件 require('rndChinaName.class.php'); $name_obj = new rndChinaName(); $name = $name_obj->getName(2); echo $name;
Recommended tutorial: "PHP Video Tutorial"
The above is the detailed content of Detailed explanation of PHP's class for randomly generating Chinese names. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Naming conventions in PHP: How to use camelCase notation to name classes, methods, and variables In PHP programming, good naming conventions are an important coding practice. It improves code readability and maintainability, and makes teamwork smoother. In this article, we will explore a common naming convention: camelCase and provide some examples of how to use it in PHP to name classes, methods, and variables. 1. What is camel case nomenclature? CamelCase is a common naming convention in which the first letter of each word is capitalized,

PHP error: Unable to declare class repeatedly, solution! It is common for developers to encounter problems. In PHP development, we often encounter a common error: the class cannot be declared repeatedly. This problem seems simple, but if not solved in time, the code will not execute correctly. This article will introduce the cause of this problem and provide a solution for your reference. When we define a class in PHP code, if the same class is defined multiple times in the same file or multiple files, an error that the class cannot be declared repeatedly will occur. This is

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,

What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more

During the Java development process, sometimes you will encounter an error: java.lang.ClassNotFoundException. It says that the required class file cannot be found in the Java Virtual Machine (JVM). This error will cause the program to not run properly, and if not resolved in time, will delay the development progress. This article will introduce the reasons and solutions for classes not found in Java. 1. Reason 1. The class path is wrong. In Java, the package path and class path are very important. If the classpath is set incorrectly or the class file

How to use Attributes to add custom annotations to classes in PHP8? Custom annotations are a way to add metadata to a class or method, which can help us obtain and process additional information on a specific class or method at runtime. In PHP8, the concept of Attributes was introduced, which allows us to easily add custom annotations to classes. This article will introduce how to use Attributes to implement custom annotations for classes in PHP8 and provide specific code examples. In PHP8, since

PHP code encapsulation skills: How to use classes and objects to encapsulate reusable code blocks Summary: During development, we often encounter code blocks that need to be reused. In order to improve the maintainability and reusability of the code, we can use class and object encapsulation techniques to encapsulate these code blocks. This article explains how to use classes and objects to encapsulate reusable blocks of code and provides several concrete code examples. Advantages of using classes and objects to encapsulate. Using classes and objects to encapsulate has the following advantages: 1.1 Improve the maintainability of code by reducing duplication

This article brings you relevant knowledge about PHP, which mainly introduces the relevant content of automatic class loading. Let's analyze the relevant files of automatic class loading in PHP. I hope it will be helpful to everyone.
