繁体   English   中英

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

[英]Can I use geoip_country_code_by_name to serve specific content to more than one country?

目前我正在使用 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM