The output of the http_accept variable is a mess, I can’t work with it, I need to convert it to an array like this:
Array
(
[en-ca] => 1
[pt-pt] => 0.4
[de] => 0.2
);
Hope you guys can help me.
EDIT:
Here is the code that will determine the preferred language of the client, this is a quick way to guess the language of the user:
I thought of all the Spanish and Portuguese visitors, there are some substrates in the middle, because pt-BR or pt-PT will use the same language files, the same for es-ES and es-CO for example.
<?php
$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 is not possible without the help of Aif (Many thanks 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);
?>