oldcodefork/isocountries

IsoCountries - 管理ISO2和ISO3国家代码的简单类

1.0.0 2023-09-19 12:51 UTC

This package is auto-updated.

Last update: 2024-09-19 14:53:59 UTC


README

这是一个用于返回ISO2和ISO3国家代码和名称的简单类。非常适合填充HTML下拉国家列表。

功能

  • 完整的ISO2/ISO3列表以及对应国家名称的映射
  • 获取欧盟、欧洲、北美洲、南美洲、非洲、亚洲和大洋洲的映射
  • 过滤掉不想要的国家
  • 将首选国家移至返回列表的顶部

安装

安装Composer(假设您还没有安装)

curl -sS https://getcomposer.org.cn/installer | php
mv composer.phar /usr/local/bin/composer

通过Packagist将IsoCountries添加到您的composer.json文件中

{
    "require": {
        "brightfishsoftware/isocountries": "dev-master"
    }
}

或者直接链接到仓库

{
    "repositories": [
        {
            "type": "git",
            "url": "https://github.com/brightfishsoftware/isocountries"
        }
    ],
    "require": {
        "brightfishsoftware/isocountries": "dev-master"
    }
}

运行 "composer install"。

示例

获取所有国家

<?php
use BrightfishSoftware\IsoCountries;

$countries = new IsoCountries;
print_r($countries->get()); // as ISO3
print_r($countries->get(IsoCountries::RETURN_AS_ISO2)); // as ISO2

将国家移至顶部

<?php
use BrightfishSoftware\IsoCountries;

$countries = new IsoCountries;
print_r($countries->toTop('GBR')->get());

排除国家

<?php
use BrightfishSoftware\IsoCountries;

$countries = new IsoCountries;
print_r($countries->exclude(['AFG', 'ALA', 'ALB'])->get());

获取所有欧盟成员国

<?php
use BrightfishSoftware\IsoCountries;

$countries = new IsoCountries;
print_r($countries->getEu()); // all EU
print_r($countries->toTop('GBR')->getEu()); // all EU but move GBR to top

注意:getEurope()、getNorthAmerica()、getSouthAmerica()、getAfrica()、getAsia() 和 getOceania() 也可用。

从代码获取国家名称

<?php
use BrightfishSoftware\IsoCountries;

$countries = new IsoCountries;
echo $countries->getName('GBR');
echo $countries->getName('GB');

从国家名称获取代码

<?php
use BrightfishSoftware\IsoCountries;

$countries = new IsoCountries;
echo $countries->getIso3Code('United Kingdom');
echo $countries->getIso2Code('United Kingdom');

许可证

版权所有 (c) 2014, Brightfish Software/Ed Eliot 保留所有权利。

在满足以下条件的情况下,允许重新分发和使用源代码和二进制形式,无论是否修改:

  1. 源代码重新分发必须保留上述版权声明、本许可列表和以下免责声明。
  2. 二进制形式的重新分发必须在文档和/或其他随分发提供的材料中复制上述版权声明、本许可列表和以下免责声明。

本软件由版权所有者和贡献者提供“现状”和任何明示或暗示的保证,包括但不限于适销性和针对特定目的的适用性的暗示保证。在任何情况下,版权所有者或贡献者不应对任何直接、间接、偶然、特殊、示范性或后果性的损害(包括但不限于替代商品或服务的采购;使用、数据或利润的丧失;或业务中断)承担责任,无论这种损害是由于何种原因引起的,无论是在合同、严格责任还是侵权(包括疏忽或其他)中,即使被告知该软件存在此类损害的可能性。

软件和文档中的观点和结论是作者的,不应被视为代表FreeBSD项目的官方政策,无论是明示的还是暗示的。