<?php function detectLanguage(array $langs, string $header): ?string { $s = strtolower($header); // case insensitive $s = strtr($s, '_', '-'); // cs_CZ means cs-CZ rsort($langs); // first more specific preg_match_all('#(' . implode('|', $langs) . ')(?:-[^\s,;=]+)?\s*(?:;\s*q=([0-9.]+))?#', $s, $matches); if (!$matches[0]) { return null; } $max = 0; $lang = null; foreach ($matches[1] as $key => $value) { $q = $matches[2][$key] === '' ? 1.0 : (float) $matches[2][$key]; if ($q > $max) { $max = $q; $lang = $value; } } return $lang; } echo detectLanguage(['en', 'fr', 'zn'], 'cs,en;q=0.9,fr;q=0.8,zh-CN;q=0.7,zh;q=0.6');
You have javascript disabled. You will not be able to edit any code.