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