Home > Backend Development > PHP Tutorial > A fun gender judgment extension in PHP

A fun gender judgment extension in PHP

Release: 2023-04-10 09:36:02
forward
3828 people have browsed it

A fun gender judgment extension in PHP

The extension we are going to learn today is also a very niche extension. In fact, to put it bluntly, maybe no one uses it at all. Expand. Of course, we are just for learning purposes to see what this extension is and what is interesting about it.

Extension description

The Gender extension can be seen from the name. It is a gender extension. This extension can be based on the user name passed to it and the user's location. Isn’t it very interesting to return gender status in regional countries? This extension can be downloaded and installed directly from the PECL website, and there are no other special requirements.

The Gender extension is used based on the gender.c function library written by the great master Joerg Michael. It is said to contain more than 40,000 names in 54 countries and regions. For specific situations, let's take a closer look at its effect in the test code below.

Latest class opening information: The 16th period of PHP full stack is online, and classes will start at 20:00 (6.28) tonight! For details, see: https://www.php.cn/k.html

Judge gender based on name

Using this When expanding, we need to prepare a function first, which is actually to facilitate our subsequent testing 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;
    }
}
Copy after login

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 determination value returned by the $gender object based on 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
Copy after login

The next step is to simply test. First, test an English name and specify the country and region 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 means that this is a male name. The content returned by the country() function is the full name and abbreviation information of the country. In the second test, we were given the name of a Japanese woman (Ayumi Hamasaki’s English name), and you can see that the 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 Chinese pinyin names, and if the specified country and region is CHINA, the pinyin names returned are neutral names (regardless of gender) name used).

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
Copy after login

Well, it seems to be a trap. This thing has no practical use for us. Since there is very little information on this extension, and the Chinese-oriented information is completely unavailable, I don’t know whether it determines the Chinese name based on pinyin. Complete Chinese will directly return NAME_NOT_FOUND information. So, just have fun everyone!

Summary

I have to say that if I hadn’t refreshed the documentation, I wouldn’t have known that PHP even provided such an extension library. In fact, interested students can try to write a similar extension library that can determine Chinese names. Although it may not be commonly used in actual business development, it can be used as an open source practice to improve one's C language. technology.

测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202011/source/1.PHP中一个好玩的性别判断扩展.php
参考文档:
https://www.php.net/manual/zh/book.gender.php
Copy after login

The above is the detailed content of A fun gender judgment extension in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:ZyBlog
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template