Transform Accept-language output in array
<p>The variable http_accept output is a mess, i can't work with it, need to transform into an array like this:</p>

<pre><code>Array
(
    [en-ca] =&gt; 1
    [pt-pt] =&gt; 0.4
    [de] =&gt; 0.2
);
</code></pre>

<p>I hope you guys can help me.</p>

<p>EDIT: </p>

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

<p>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.</p>

<pre><code>&lt;?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 =&gt; $val) { 
      if(substr($key, 0, 2) == "pt"){
        echo "Portugues e preferido aqui";}
}   

?&gt;
</code></pre>

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

<pre><code>&lt;?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);
?&gt;
</code></pre>
