Skip to content

Instantly share code, notes, and snippets.

@spolischook
Last active November 15, 2023 04:35
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save spolischook/0cde9c6286415cddc088 to your computer and use it in GitHub Desktop.
Save spolischook/0cde9c6286415cddc088 to your computer and use it in GitHub Desktop.
Get prefer language by parsing HTTP_ACCEPT_LANGUAGE header
<?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
*/
@Koenvh1
Copy link

Koenvh1 commented Apr 13, 2017

Very nice and simple solution 👍

@calvinclaus
Copy link

Thank you very much <3

@fabswt
Copy link

fabswt commented Nov 20, 2020

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