dtbi27903 2011-05-29 15:17
浏览 17
已采纳

转换数组中的Accept-language输出

The variable http_accept output is a mess, i can't work with it, need to transform into an array like this:

Array
(
    [en-ca] => 1
    [pt-pt] => 0.4
    [de] => 0.2
);

I hope you guys can help me.

EDIT:

Here is a code that will detect the preferred language of the client, this is a quick way to guess the user's language:

I've done this thinking of all Spanish and Portuguese visitors, there are some substr in the midle, because pt-BR or pt-PT will use the same language files, the same for es-ES and es-CO for example.

<?php
// detectar idioma
$linguagens = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$parcelas = explode(',', $linguagens);
$linguas_aceites = array();

foreach ($parcelas as $lingua) {
        $contagem = preg_match('/([-a-zA-Z]+)\s*;\s*q=([0-9\.]+)/', $lingua, $matches);
        if ($contagem === 0)
           $linguas_aceites[substr($lingua, 0, 2)] = 1;
        else
           $linguas_aceites[substr($matches[1], 0, 2)] = $matches[2];
}

foreach ($linguas_aceites as $key => $val) { 
      if(substr($key, 0, 2) == "pt"){
        echo "Portugues e preferido aqui";}
}   

?>

This wouldn't be possible without Aif's help (Thanks a lot for the original code!):

<?php

    $al = 'en-US,en;q=0.8,pt-PT;q=0.6';
    $values = explode(',', $al);

    $accept_language = array();

    foreach ($values AS $lang) {
            $cnt = preg_match('/([-a-zA-Z]+)\s*;\s*q=([0-9\.]+)/', $lang, $matches);
            if ($cnt === 0)
               $accept_language[$lang] = 1;
            else
               $accept_language[$matches[1]] = $matches[2];
    }

    print_r($accept_language);
?>
  • 写回答

1条回答 默认 最新

  • doupao2277 2011-05-29 15:21
    关注

    Look likes this is your answer :)

    Attempt to explain one algorithm: (tested only with your sample)

    // Get the accept-language header
    $al = 'en-US,en;q=0.8,pt-PT;q=0.6'
    

    Notice that each lang is separated by a comma , and for some language, you have an order of preference (the float stuff).

    First, get the values inside the commas:

    $values = explode(',', $al);
    

    Then for each value, if there is a ;, retrieve the value for the left hand key and store it in an array; otherwize, store the lang as a key, and take 1 as a value. I use the fact that preg_match returns the number of match (then 0 if none, in our case, the first one).

    foreach ($values AS $lang) {
           $cnt = preg_match('/([-a-zA-Z]+)\s*;\s*q=([0-9\.]+)/', $lang, $matches);
           if ($cnt === 0)
               $accept_language[$lang] = 1;
           else
               $accept_language[$matches[1]] = $matches[2];
    }
    

    The whole script:

    <?php
    
    $al = 'en-US,en;q=0.8,pt-PT;q=0.6';
    $values = explode(',', $al);
    
    $accept_language = array();
    
    foreach ($values AS $lang) {
            $cnt = preg_match('/([-a-zA-Z]+)\s*;\s*q=([0-9\.]+)/', $lang, $matches);
            if ($cnt === 0)
               $accept_language[$lang] = 1;
            else
               $accept_language[$matches[1]] = $matches[2];
    }
    
    print_r($accept_language);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路