Last active
November 9, 2024 05:34
-
-
Save spolischook/0cde9c6286415cddc088 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 | |
*/ |
Thank you very much <3
Thanks.
I found this a useful read, too: https://developer.mozilla.org/en-US/docs/Glossary/quality_values
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice and simple solution 👍