The extension we are going to study today is also a very niche extension. In fact, to put it bluntly, perhaps no one has used the extension at all. Of course, we are just looking at what this extension is for the purpose of learning, and what is interesting about it.
Extended description
Gender extension can be seen from the name, it is an extension about gender, this extension can return the gender based on the user name passed to it and the country where the user is located, is it very interesting? This extension can be downloaded and installed directly on the PECL website, there are no other special requirements.
The Gender extension is used according to the function library gender.c written by the great god Joerg Michael, which is said to contain more than 40,000 names in 54 countries and regions. For the specific situation, let's take a good look at its effect in the following test code.
Determine gender based on name
When using this extension, we must first prepare a function, which is actually convenient for our later test operations.
$gender = new Gender\Gender;
function showGender($name, $country)
{
global $gender;
$result = $gender->get($name, $country);
var_dump($result);
$data = $gender->country($country);
var_dump($data);
switch ($result) {
case Gender\Gender::IS_FEMALE:
printf("%s:女性 - %s\n", $name, $data['country']);
break;
case Gender\Gender::IS_MOSTLY_FEMALE:
printf("%s:大部分情况下是女性 - %s\n", $name, $data['country']);
break;
case Gender\Gender::IS_MALE:
printf("%s:男性 - %s\n", $name, $data['country']);
break;
case Gender\Gender::IS_MOSTLY_MALE:
printf("%s:大部分情况下是男性 - %s\n", $name, $data['country']);
break;
case Gender\Gender::IS_UNISEX_NAME:
printf("%s:中性名称(不好确认性别) - \n", $name, $data['country']);
break;
case Gender\Gender::IS_A_COUPLE:
printf("%s:男女都适用 - %s\n", $name, $data['country']);
break;
case Gender\Gender::NAME_NOT_FOUND:
printf("%s:对应的国家字典中没有找到相关信息 - %s\n", $name, $data['country']);
break;
case Gender\Gender::ERROR_IN_NAME:
echo "给定的姓名信息错误\n";
break;
default:
echo "错误!\n";
break;
}
}
First, instantiate a Gender object, and then define a function. In this function, we print the information returned by the get() method in the Gender class. The information it returns is the gender judgment value returned by the $gender object according to the specified parameters. Then we use the following switch to determine whether the returned value corresponds to the constant in the class, so that we know what the returned result is. The country() method obtains the detailed information of the specified country and region, and we can see their output in the following test code.
showGender("William", Gender\Gender::USA);
// int(77)
// array(2) {
// ["country_short"]=>
// string(3) "USA"
// ["country"]=>
// string(6) "U.S.A."
// }
// William:男性 - U.S.A.
showGender("Ayumi Hamasaki", Gender\Gender::JAPAN);
// int(70)
// array(2) {
// ["country_short"]=>
// string(3) "JAP"
// ["country"]=>
// string(5) "Japan"
// }
// Ayumi Hamasaki:女性 - Japan
The next step is to simply test, first test an English name, and specify the country as USA, which is the United States. You can see that the value returned by get() is 77, which corresponds to the constant value of IS_MALE, which is the name of a male. The content returned by the country() function is the full name and abbreviated information of the country. In the second part of the test, we were given the name of a Japanese woman (Ayumi Hamasaki's English name), and we can see that information about the woman and country was returned normally. So, does this extension support Chinese?
Sorry, it does not support Chinese, so we need to use the Chinese pinyin name, and if the specified country is CHINA, the pinyin name returned is a neutral name (a name that can be used regardless of gender).
howGender("Gang Qiang", Gender\Gender::CHINA);
// int(63)
// array(2) {
// ["country_short"]=>
// string(3) "CHN"
// ["country"]=>
// string(5) "China"
// }
// Gang Qiang:中性名称(不好确认性别)
showGender("Anna Li", Gender\Gender::CHINA);
// int(70)
// array(2) {
// ["country_short"]=>
// string(3) "CHN"
// ["country"]=>
// string(5) "China"
// }
// Anna Li:女性 - China
Uh, it looks like a pit, this thing is of no practical use to us. Because this extension has very little information, and the Chinese-oriented information is completely unavailable, I don't know if it determines the Chinese name based on pinyin. Complete Chinese will directly return NAME_NOT_FOUND information. So, just have fun with everyone!
Summarize
I have to say, if you don't have the documentation, you really don't know that PHP also provides such an extension library. In fact, interested students can try to write a similar extension library that can judge Chinese names. Although it may not be commonly used in actual business development, it can be used as an open source mobile phone to improve their C language. Technology.
Test code:
Reference documents:
https://www.php.net/manual/zh/book.gender.php
Searchable on their respective media platforms [Hardcore Project Manager]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。