Souza
Souza

Reputation: 1143

Transform Accept-language output in array

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);
?>

Upvotes: 3

Views: 2077

Answers (1)

Aif
Aif

Reputation: 11220

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);

Upvotes: 7

Related Questions