Forked from spolischook/kotoblog_parse-http-accept-language-header.php
Created
September 21, 2020 06:35
-
-
Save enix-app/c97ed207b8af31530650f2549d246d46 to your computer and use it in GitHub Desktop.
Get prefer language by parsing HTTP_ACCEPT_LANGUAGE header
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$prefLocales = array_reduce( | |
explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']), | |
function ($res, $el) { | |
list($l, $q) = array_merge(explode(';q=', $el), [1]); | |
$res[$l] = (float) $q; | |
return $res; | |
}, []); | |
arsort($prefLocales); | |
/* | |
This get you from headers like this | |
string 'en-US,en;q=0.8,uk;q=0.6,ru;q=0.4' (length=32) | |
array like this | |
array (size=4) | |
'en-US' => float 1 | |
'en' => float 0.8 | |
'uk' => float 0.6 | |
'ru' => float 0.4 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment