【问题标题】:Can I use geoip_country_code_by_name to serve specific content to more than one country?我可以使用 geoip_country_code_by_name 向多个国家/地区提供特定内容吗?
【发布时间】:2016-10-02 08:10:55
【问题描述】:

目前我正在使用 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 不同的默认内容数组。

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

【问题讨论】:

  • 你可以有一个默认条目。通过引用您想要的默认值来分配它/
  • @atoms 我如何“通过引用分配它”?
  • 我不确定,我想我可能有一个问题要问自己。我将回答这个关于如何没有

标签: php html geoip


【解决方案1】:
$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相当陌生,所以我只有一个简单的问题。这段代码是你写的吗:define("DEFAULT_LANGUAGE", 'GB');我在 php 中定义我的默认语言的实际方式是什么?
  • 没问题的沙人。 define 关键字允许您设置一个字符串,在这种情况下,文本GB 可以在该脚本/文件的运行时访问。见php.net/manual/en/function.define.php,我也更新了答案,最后一段代码应该可以解决你的问题
  • 哦,好吧,这太简单了,多么棒的解决方案!是否有一个特定的文件应该放置最后一段代码?或者它可以从任何地方运行吗?如果这是另一个简单的问题,我再次道歉。
  • 已更新代码。我会将定义放在 config.php 之类的文件中,并将其包含在每个页面的顶部。我也会做不同的事情并检查一次语言,然后请求该语言。这是随着您网站的增长,您不会加载许多不同的信息副本。但是对于您当前的应用程序,我会在您请求语言之前检查
  • 令人难以置信的对细节的关注和非常有用的答案。你已经从前端开发人员的生活中解脱了很多压力,他把自己混在后端编码中哈哈谢谢你,谢谢你,谢谢你的时间和你的工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多