Skip to content

Instantly share code, notes, and snippets.

@thg2k
Created February 21, 2020 13:32
Show Gist options
  • Save thg2k/8ce85746ee50de4a773bab8a984b7f4e to your computer and use it in GitHub Desktop.
Save thg2k/8ce85746ee50de4a773bab8a984b7f4e to your computer and use it in GitHub Desktop.
Parse HTTP header Accept-Language
<?php
function get_http_accept_language($accept_language = null) {
if (($accept_language == "") && isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
$accept_language = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
$langs = array();
if (preg_match_all('/([a-z]{1,8}(?:-[a-z]{1,8})?)\s*(?:;\s*q\s*=\s*(1|0\.[0-9]+))?/i',
$accept_language, $regp)) {
for ($i = 0; $i < count($regp[0]); $i++) {
$langs[$regp[1][$i]] = (float) ($regp[2][$i] ?: 1);
}
arsort($langs);
}
return $langs;
}
var_dump(get_http_accept_language());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment