<?php
/**
* Get a sorted list of client accepted languages.
*
* @staticvar array $examined
* Used to cache the sorted list for <var>$accept_languages</var> strings.
* @param boolean $list [optional]
* Whether to get a sorted list or the language with the highest preference, defaults to <code>FALSE</code> and
* the language with the highest preference will be returned.
* @param string $accept_languages [optional]
* The client submitted accept languages string in the format defined in RFC 2616.
* @param string $default [optional]
* The default ISO 639-1 alpha-2 language code to use if no other language could be determined, defaults to
* <code>"en"</code>.
* @return array|string
* Associative array where the key is the language code or locale and the value the preference if <var>$list</var>
* is set to <code>TRUE</code>, otherwise the ISO 639-1 alpha-2 code or locale of the language with the highest
* preference.
*/
function get_client_preferred_language($list = false, $accept_languages = null, $default = "en") {
static $examined = array();
// Either use the supplied string or use the string from server input, this construct ensures highest performance
// because a positive check is performed.
$accept_languages || ($accept_languages = filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH));
// Only examine this string if we haven't done so in the past.
if (empty($examined[$accept_languages])) {
// Prepare default fallback in our cache for this string.
$examined[$accept_languages] = array($default => 1.0);
// Only continue if we have a string to examine.
if ($accept_languages) {
// Prepare array for collecting the preferred languages.
$languages = array();
// Extract all languages from the accept languages string, the format is defined in RFC 2616.
preg_match_all("/([a-z]+(?:-[a-z]+)?)\s*(?:;\s*q\s*=\s*(1|0?\.[0-9]+))?/i", $accept_languages, $matches);
// Only go through all extracted languages if we have any.
if (!empty($matches[1])) {
foreach ($matches[1] as $delta => $code) {
// Empty means highest preference.
$languages[$code] = empty($matches[2][$delta]) ? 1.0 : (float) $matches[2][$delta];
}
// Sort the accepted languages by preference, if sorting fails use default.
if (arsort($languages, SORT_NUMERIC) === true) {
$examined[$accept_languages] = $languages;
}
}
}
}
return $list ? $examined[$accept_languages] : resetarray_values($examined[$accept_languages]);[0];
}
// ----------------------------------------------------------------------------------------------------------------- Test
function get_client_preferred_language_test() {
assert(array(
"en-US" => 1.0,
"en" => 1.0,
"en-AU" => 0.8,
"fr" => 0.6,
"en-GB" => 0.4,
) === get_client_preferred_language(true, "en,en-US,en-AU;q=0.8,fr;q=0.6,en-GB;q=0.4"));
}
get_client_preferred_language_test();
<?php
/**
* Get a sorted list of client accepted languages.
*
* @staticvar array $examined
* Used to cache the sorted list for <var>$accept_languages</var> strings.
* @param boolean $list [optional]
* Whether to get a sorted list or the language with the highest preference, defaults to <code>FALSE</code> and
* the language with the highest preference will be returned.
* @param string $accept_languages [optional]
* The client submitted accept languages string in the format defined in RFC 2616.
* @param string $default [optional]
* The default ISO 639-1 alpha-2 language code to use if no other language could be determined, defaults to
* <code>"en"</code>.
* @return array|string
* Associative array where the key is the language code or locale and the value the preference if <var>$list</var>
* is set to <code>TRUE</code>, otherwise the ISO 639-1 alpha-2 code or locale of the language with the highest
* preference.
*/
function get_client_preferred_language($list = false, $accept_languages = null, $default = "en") {
static $examined = array();
// Either use the supplied string or use the string from server input, this construct ensures highest performance
// because a positive check is performed.
$accept_languages || ($accept_languages = filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH));
// Only examine this string if we haven't done so in the past.
if (empty($examined[$accept_languages])) {
// Prepare default fallback in our cache for this string.
$examined[$accept_languages] = array($default => 1.0);
// Only continue if we have a string to examine.
if ($accept_languages) {
// Prepare array for collecting the preferred languages.
$languages = array();
// Extract all languages from the accept languages string, the format is defined in RFC 2616.
preg_match_all("/([a-z]+(?:-[a-z]+)?)\s*(?:;\s*q\s*=\s*(1|0?\.[0-9]+))?/i", $accept_languages, $matches);
// Only go through all extracted languages if we have any.
if (!empty($matches[1])) {
foreach ($matches[1] as $delta => $code) {
// Empty means highest preference.
$languages[$code] = empty($matches[2][$delta]) ? 1.0 : (float) $matches[2][$delta];
}
// Sort the accepted languages by preference, if sorting fails use default.
if (arsort($languages, SORT_NUMERIC) === true) {
$examined[$accept_languages] = $languages;
}
}
}
}
return $list ? $examined[$accept_languages] : reset($examined[$accept_languages]);
}
// ----------------------------------------------------------------------------------------------------------------- Test
function get_client_preferred_language_test() {
assert(array(
"en-US" => 1.0,
"en" => 1.0,
"en-AU" => 0.8,
"fr" => 0.6,
"en-GB" => 0.4,
) === get_client_preferred_language(true, "en,en-US,en-AU;q=0.8,fr;q=0.6,en-GB;q=0.4"));
}
get_client_preferred_language_test();
<?php
/**
* Get a sorted list of client accepted languages.
*
* @staticvar array $examined
* Used to cache the sorted list for <var>$accept_languages</var> strings.
* @param boolean $list [optional]
* Whether to get a sorted list or the language with the highest preference, defaults to <code>FALSE</code> and
* the language with the highest preference will be returned.
* @param string $accept_languages [optional]
* The client submitted accept languages string in the format defined in RFC 2616.
* @param string $default [optional]
* The default ISO 639-1 alpha-2 language code to use if no other language could be determined, defaults to
* <code>"en"</code>.
* @return array|string
* Associative array where the key is the language code or locale and the value the preference if <var>$list</var>
* is set to <code>TRUE</code>, otherwise the ISO 639-1 alpha-2 code or locale of the language with the highest
* preference.
*/
function get_client_preferred_language($list = false, $accept_languages = null, $default = "en") {
static $examined = array();
// Either use the supplied string or use the string from server input, this construct ensures highest performance
// because a positive check is performed.
$accept_languages || ($accept_languages = filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH));
// Only examine this string if we haven't done so in the past.
if (empty($examined[$accept_languages])) {
// Prepare default fallback in our cache for this string.
$examined[$accept_languages] = array($default => 1.0);
// Only continue if we have a string to examine.
if ($accept_languages) {
// Prepare array for collecting the preferred languages.
$languages = array();
// Extract all languages from the accept languages string, the format is defined in RFC 2616.
preg_match_all("/([a-z]+(?:-[a-z]+)?)\s*(?:;\s*q\s*=\s*(1|0?\.[0-9]+))?/i", $accept_languages, $matches);
// Only go through all extracted languages if we have any.
if (!empty($matches[1])) {
foreach ($matches[1] as $delta => $code) {
// Empty means highest preference.
$languages[$code] = empty($matches[2][$delta]) ? 1.0 : (float) $matches[2][$delta];
}
// Sort the accepted languages by preference, if sorting fails use default.
if (arsort($languages, SORT_NUMERIC) === true) {
$examined[$accept_languages] = $languages;
}
}
}
}
return $list ? $examined[$accept_languages] : array_values($examined[$accept_languages])[0];
}
// ----------------------------------------------------------------------------------------------------------------- Test
function get_client_preferred_language_test() {
assert(array(
"en-US" => 1.0,
"en" => 1.0,
"en-AU" => 0.8,
"fr" => 0.6,
"en-GB" => 0.4,
) === get_client_preferred_language(true, "en,en-US,en-AU;q=0.8,fr;q=0.6,en-GB;q=0.4"));
}
get_client_preferred_language_test();
<?php
/**
* Get a sorted list of client accepted languages.
*
* @staticvar array $examined
* Used to cache the sorted list for <var>$accept_languages</var> strings.
* @param boolean $list [optional]
* Whether to get a sorted list or the language with the highest preference, defaults to <code>FALSE</code> and
* the language with the highest preference will be returned.
* @param string $accept_languages [optional]
* The client submitted accept languages string in the format defined in RFC 2616.
* @param string $default [optional]
* The default ISO 639-1 alpha-2 language code to use if no other language could be determined, defaults to
* <code>"en"</code>.
* @return array|string
* Associative array where the key is the language code or locale and the value the preference if <var>$list</var>
* is set to <code>TRUE</code>, otherwise the ISO 639-1 alpha-2 code or locale of the language with the highest
* preference.
*/
function get_client_preferred_language($list = false, $accept_languages = null, $default = "en") {
static $examined = array();
// Either use the supplied string or use the string from server input, this construct ensures highest performance
// because a positive check is doneperformed.
$accept_languages || ($accept_languages = filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH));
// Check if weOnly alreadyexamine examinedthis thestring preferredif languageswe forhaven't thisdone stringso andin directlythe returnpast.
if (issetempty($examined[$accept_languages])) {
return// $listPrepare ?default fallback in our cache for this string.
$examined[$accept_languages] := resetarray($examined[$accept_languages]$default => 1.0);
}
// Only continue if we have a string to examine.
if ($accept_languages) {
// Prepare array for collecting the acceptedpreferred languages.
$languages = array();
// Extract all languages from the accept languagelanguages string, the format is defined in RFC 2616.
preg_match_all("/([a-z]+(?:-[a-z]+)?)\s*(?:;\s*q\s*=\s*(1|0?\.[0-9]+))?/i", $accept_languages, $matches);
// Only go through all extracted languages if we have any, note that the else condition falls through to the.
// return of the default language.
if (!empty($matches[1])) {
// Go through all extracted languages.
foreach ($matches[1] as $delta => $code) {
// As per RFC the preference might be empty,// useEmpty defaultmeans highest preference in that case. If non-empty cast
// to float for sorting.
$languages[$code] = empty($matches[2][$delta]) ? 1.0 : (float) $matches[2][$delta];
}
// Sort the accepted languages by preference, if sorting fails fall through to returning theuse default.
if (arsort($languages, SORT_NUMERIC) === true) {
// Cache the examined and sorted list.
$examined[$accept_languages] = $languages;
// Either return the complete sorted list or the first language from the list.
return $list ? $languages : reset($languages);}
}
}
}
return $list ? array($default => 1.0)$examined[$accept_languages] : $default;reset($examined[$accept_languages]);
}
// ----------------------------------------------------------------------------------------------------------------- Test
function get_client_preferred_language_test() {
assert(array(
"en-US" => 1.0,
"en" => 1.0,
"en-AU" => 0.8,
"fr" => 0.6,
"en-GB" => 0.4,
) === get_client_preferred_language(true, "en,en-US,en-AU;q=0.8,fr;q=0.6,en-GB;q=0.4"));
}
get_client_preferred_language_test();
<?php
/**
* Get a sorted list of client accepted languages.
*
* @staticvar array $examined
* Used to cache the sorted list for <var>$accept_languages</var> strings.
* @param boolean $list [optional]
* Whether to get a sorted list or the language with the highest preference, defaults to <code>FALSE</code> and
* the language with the highest preference will be returned.
* @param string $accept_languages [optional]
* The client submitted accept languages string in the format defined in RFC 2616.
* @param string $default [optional]
* The default ISO 639-1 alpha-2 language code to use if no other language could be determined, defaults to
* <code>"en"</code>.
* @return array|string
* Associative array where the key is the language code or locale and the value the preference if <var>$list</var>
* is set to <code>TRUE</code>, otherwise the ISO 639-1 alpha-2 code or locale of the language with the highest
* preference.
*/
function get_client_preferred_language($list = false, $accept_languages = null, $default = "en") {
static $examined = array();
// Either use the supplied string or use the string from server input, this construct ensures highest performance
// because a positive check is done.
$accept_languages || ($accept_languages = filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH));
// Check if we already examined the preferred languages for this string and directly return.
if (isset($examined[$accept_languages])) {
return $list ? $examined[$accept_languages] : reset($examined[$accept_languages]);
}
// Only continue if we have a string to examine.
if ($accept_languages) {
// Prepare array for collecting the accepted languages.
$languages = array();
// Extract all languages from the accept language string, the format is defined in RFC 2616.
preg_match_all("/([a-z]+(?:-[a-z]+)?)\s*(?:;\s*q\s*=\s*(1|0?\.[0-9]+))?/i", $accept_languages, $matches);
// Only go through all extracted languages if we have any, note that the else condition falls through to the
// return of the default language.
if (!empty($matches[1])) {
// Go through all extracted languages.
foreach ($matches[1] as $delta => $code) {
// As per RFC the preference might be empty, use default highest preference in that case. If non-empty cast
// to float for sorting.
$languages[$code] = empty($matches[2][$delta]) ? 1.0 : (float) $matches[2][$delta];
}
// Sort the accepted languages by preference, if sorting fails fall through to returning the default.
if (arsort($languages, SORT_NUMERIC) === true) {
// Cache the examined and sorted list.
$examined[$accept_languages] = $languages;
// Either return the complete sorted list or the first language from the list.
return $list ? $languages : reset($languages);
}
}
}
return $list ? array($default => 1.0) : $default;
}
// ----------------------------------------------------------------------------------------------------------------- Test
function get_client_preferred_language_test() {
assert(array(
"en-US" => 1.0,
"en" => 1.0,
"en-AU" => 0.8,
"fr" => 0.6,
"en-GB" => 0.4,
) === get_client_preferred_language(true, "en,en-US,en-AU;q=0.8,fr;q=0.6,en-GB;q=0.4"));
}
get_client_preferred_language_test();
<?php
/**
* Get a sorted list of client accepted languages.
*
* @staticvar array $examined
* Used to cache the sorted list for <var>$accept_languages</var> strings.
* @param boolean $list [optional]
* Whether to get a sorted list or the language with the highest preference, defaults to <code>FALSE</code> and
* the language with the highest preference will be returned.
* @param string $accept_languages [optional]
* The client submitted accept languages string in the format defined in RFC 2616.
* @param string $default [optional]
* The default ISO 639-1 alpha-2 language code to use if no other language could be determined, defaults to
* <code>"en"</code>.
* @return array|string
* Associative array where the key is the language code or locale and the value the preference if <var>$list</var>
* is set to <code>TRUE</code>, otherwise the ISO 639-1 alpha-2 code or locale of the language with the highest
* preference.
*/
function get_client_preferred_language($list = false, $accept_languages = null, $default = "en") {
static $examined = array();
// Either use the supplied string or use the string from server input, this construct ensures highest performance
// because a positive check is performed.
$accept_languages || ($accept_languages = filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH));
// Only examine this string if we haven't done so in the past.
if (empty($examined[$accept_languages])) {
// Prepare default fallback in our cache for this string.
$examined[$accept_languages] = array($default => 1.0);
// Only continue if we have a string to examine.
if ($accept_languages) {
// Prepare array for collecting the preferred languages.
$languages = array();
// Extract all languages from the accept languages string, the format is defined in RFC 2616.
preg_match_all("/([a-z]+(?:-[a-z]+)?)\s*(?:;\s*q\s*=\s*(1|0?\.[0-9]+))?/i", $accept_languages, $matches);
// Only go through all extracted languages if we have any.
if (!empty($matches[1])) {
foreach ($matches[1] as $delta => $code) {
// Empty means highest preference.
$languages[$code] = empty($matches[2][$delta]) ? 1.0 : (float) $matches[2][$delta];
}
// Sort the accepted languages by preference, if sorting fails use default.
if (arsort($languages, SORT_NUMERIC) === true) {
$examined[$accept_languages] = $languages;
}
}
}
}
return $list ? $examined[$accept_languages] : reset($examined[$accept_languages]);
}
// ----------------------------------------------------------------------------------------------------------------- Test
function get_client_preferred_language_test() {
assert(array(
"en-US" => 1.0,
"en" => 1.0,
"en-AU" => 0.8,
"fr" => 0.6,
"en-GB" => 0.4,
) === get_client_preferred_language(true, "en,en-US,en-AU;q=0.8,fr;q=0.6,en-GB;q=0.4"));
}
get_client_preferred_language_test();
The following is imho easier to read and should give you best performance.
<?php
/**
* Get a sorted list of client accepted languages.
*
* @staticvar array $examined
* Used to cache the sorted list for <var>$accept_languages</var> strings.
* @param boolean $list [optional]
* Whether to get a sorted list or the language with the highest preference, defaults to <code>FALSE</code> and
* the language with the highest preference will be returned.
* @param string $accept_languages [optional]
* The client submitted accept languages string in the format defined in RFC 2616.
* @param string $default [optional]
* The default ISO 639-1 alpha-2 language code to use if no other language could be determined, defaults to
* <code>"en"</code>.
* @return array|string
* Associative array where the key is the language code or locale and the value the preference if <var>$list</var>
* is set to <code>TRUE</code>, otherwise the ISO 639-1 alpha-2 code or locale of the language with the highest
* preference.
*/
function get_client_preferred_language($list = false, $accept_languages = null, $default = "en") {
static $examined = array();
// Either use the supplied string or use the string from server input, this construct ensures highest performance
// because a positive check is done.
$accept_languages || ($accept_languages = filter_input(INPUT_SERVER, "HTTP_ACCEPT_LANGUAGE", FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH));
// Check if we already examined the preferred languages for this string and directly return.
if (isset($examined[$accept_languages])) {
return $list ? $examined[$accept_languages] : reset($examined[$accept_languages]);
}
// Only continue if we have a string to examine.
if ($accept_languages) {
// Prepare array for collecting the accepted languages.
$languages = array();
// Extract all languages from the accept language string, the format is defined in RFC 2616.
preg_match_all("/([a-z]+(?:-[a-z]+)?)\s*(?:;\s*q\s*=\s*(1|0?\.[0-9]+))?/i", $accept_languages, $matches);
// Only go through all extracted languages if we have any, note that the else condition falls through to the
// return of the default language.
if (!empty($matches[1])) {
// Go through all extracted languages.
foreach ($matches[1] as $delta => $code) {
// As per RFC the preference might be empty, use default highest preference in that case. If non-empty cast
// to float for sorting.
$languages[$code] = empty($matches[2][$delta]) ? 1.0 : (float) $matches[2][$delta];
}
// Sort the accepted languages by preference, if sorting fails fall through to returning the default.
if (arsort($languages, SORT_NUMERIC) === true) {
// Cache the examined and sorted list.
$examined[$accept_languages] = $languages;
// Either return the complete sorted list or the first language from the list.
return $list ? $languages : reset($languages);
}
}
}
return $list ? array($default => 1.0) : $default;
}
// ----------------------------------------------------------------------------------------------------------------- Test
function get_client_preferred_language_test() {
assert(array(
"en-US" => 1.0,
"en" => 1.0,
"en-AU" => 0.8,
"fr" => 0.6,
"en-GB" => 0.4,
) === get_client_preferred_language(true, "en,en-US,en-AU;q=0.8,fr;q=0.6,en-GB;q=0.4"));
}
get_client_preferred_language_test();
lang-php