php - 我可以使用 geoip_country_code_by_name 向多个国家/地区提供特定内容吗?

标签 php html geoip

目前我正在使用 php geoip_country_code_by_name 函数从一个数组中为不同的国家提供不同的内容,如下所示:

<?php

    $content = array(
        'GB' => array(
            'meta_description'  => "Description is here",
            'social_title'      => "Title here",
            'country_content_js'   => "js/index.js",
        ),
        'BR' => array(
            'meta_description'  => "Different Description is here",
            'social_title'      => "Another Title here",
            'country_content_js'   => "js/index-2.js",
        ),
    );

?>

但我只有针对巴西和英国的特定内容。我希望访问该页面的任何其他国家/地区都可以使用与 BR 和 GB 不同的默认内容数组。

有没有办法创建一个规则,向我的数组中未指定的任何国家/地区提供一组默认内容?

最佳答案

$content = array(
    'GB' => array(
        'meta_description'  => "Description is here",
        'social_title'      => "Title here",
        'country_content_js'   => "js/index.js",
    ),
    'BR' => array(
        'meta_description'  => "Different Description is here",
        'social_title'      => "Another Title here",
        'country_content_js'   => "js/index-2.js",
    )
);

您可以像这样使用另一个“默认”键来引用一个键;

$content['Default'] =& $content["GB"];
var_dump($content);
exit;

或者,如果您订购了从 DB 或其他任何地方返回的值,您可以像这样读取数组的第一个条目; $aDefault =& $content[array_keys($content)[0]];

或者您可以定义默认语言并读取该数组键,但与之前的方法不同,它必须在数组中。

// define default 
define("DEFAULT_LANGUAGE", 'GB');

// would need to guarentee its there
$aDefault =& $content[DEFAULT_LANGUAGE];

最后你可以结合上面的,所以如果它找不到你可以使用第一个可用的语言;

// define, can be placed in an included config folder
define("DEFAULT_LANGUAGE", 'GB');

$content = array(
    'GBs' => array(
        'meta_description'  => "Description is here",
        'social_title'      => "Title here",
        'country_content_js'   => "js/index.js",
    ),
    'BR' => array(
        'meta_description'  => "Different Description is here",
        'social_title'      => "Another Title here",
        'country_content_js'   => "js/index-2.js",
    )
);

// does the default language exist?
if( isset($content[DEFAULT_LANGUAGE]) ){
    // yes, create a default array key and reference the required element in the array
    $content['Default'] =& $content[DEFAULT_LANGUAGE];
}else{
    // no, create a default array key and reference the first element
    $content['Default'] =& $content[array_keys($content)[0]];
}

var_dump($content);
exit;

关于php - 我可以使用 geoip_country_code_by_name 向多个国家/地区提供特定内容吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39815099/

相关文章:

php - 单击帖子时链接到另一个页面 php

geoip - 在 SQL 中查询 GeoLite2 Country CSV

php - 按连续多个日期中最旧的日期排序

PHP - 使用 fpassthru 通过 imagePNG 将图像发送到移动设备

html - 在 div 中居中图像的中间部分

javascript - Mutation Observer 无法在受支持的 Microsoft Edge 版本上运行

php - 如何删除某些 URL 的 URL 重写

c++ - C++ 的 GeoIP 有这个库吗?

elasticsearch - Geoip Logstash过滤器

javascript - 加载脚本的 AJAX 进度条