\n"); $knownlangs = array('aa', 'ab', 'af', 'am', 'ar', 'as', 'ay', 'az', 'ba', 'be', 'bg', 'bh', 'bi', 'bn', 'bo', 'br', 'ca', 'co', 'cs', 'cy', 'da', 'de', 'dz', 'el', 'en', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fj', 'fo', 'fr', 'fy', 'ga', 'gd', 'gl', 'gn', 'gu', 'ha', 'hi', 'hr', 'hu', 'hy', 'ia', 'ie', 'ik', 'in', 'is', 'it', 'iw', 'ja', 'ji', 'jw', 'ka', 'kk', 'kl', 'km', 'kn', 'ko', 'ks', 'ku', 'ky', 'la', 'ln', 'lo', 'lt', 'lv', 'mg', 'mi', 'mk', 'ml', 'mn', 'mo', 'mr', 'ms', 'mt', 'my', 'na', 'ne', 'nl', 'no', 'oc', 'om', 'or', 'pa', 'pl', 'ps', 'pt', 'qu', 'rm', 'rn', 'ro', 'ru', 'rw', 'sa', 'sd', 'sg', 'sh', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sq', 'sr', 'ss', 'st', 'su', 'sv', 'sw', 'ta', 'te', 'tg', 'th', 'ti', 'tk', 'tl', 'tn', 'to', 'tr', 'ts', 'tt', 'tw', 'uk', 'ur', 'uz', 'vi', 'vo', 'wo', 'xh', 'yo', 'zh', 'zu'); } # Initalisation of the default variables if(isset($HTTP_SERVER_VARS)) $lclist = trim($HTTP_SERVER_VARS['HTTP_ACCEPT_LANGUAGE']); # PHP running from a server else $lclist = trim($_SERVER['HTTP_ACCEPT_LANGUAGE']); # PHP 5.0 when the old server variables disabled $lctag = $deflang; $tmppos = strpos($lctag, '-'); $quality = 0.0; if($tmppos == FALSE or $tmppos == NULL) { $lang = $deflang; if($deflang == NULL) $country = NULL; else $country = ''; } else { $lang = substr($lctag, 0, $tmppos); $country = substr($lctag, $tmppos + 1); echo "lang sub = $lang"; } # $langtag = '((?:[a-zA-Z]{1,8})(?:-[a-zA-Z]{1,8})*)'; $qvalue ='(0(?:\.[0-9]{1,3})?|1(?:\.0{1,3}))'; $eachbit = '^' . $langtag . '(?:;q=' . $qvalue . ')?(?:,\s*)?(.*)$'; # $tmplclist = $lclist; # The loop while(strlen($tmplclist)) { if(preg_match("/$eachbit/", $tmplclist, $tmptagarray)) # requires 3.0.9 { # Extract data from the tag list $tmplctag = $tmptagarray[1]; $tmpquality = $tmptagarray[2]; if(strlen($tmpquality) == 0) $tmpquality = 1; $tmplclist = $tmptagarray[3]; # Find the tag in our $knownlangs array (this search includes the country code if set) if(in_array($tmplctag, $knownlangs) and $tmpquality > $quality) { # Extract the language & quality $lctag = $tmplctag; $lang = $tmplctag; $country = ''; $quality = $tmpquality; } else { # Not found, does the tag include a country code? $tmppos = strpos($tmplctag, '-'); if($tmppos <> FALSE) { # OK, the tag includes a country code but it's not in the $knownlangs array, let's extract the language and look it up if(in_array(substr($tmplctag, 0, $tmppos), $knownlangs) and $tmpquality > $quality) { # Extract the language, country & quality $lctag = $tmplctag; $lang = substr($tmplctag, 0, $tmppos); $country = substr($tmplctag, $tmppos + 1); $quality = $tmpquality; } } } } else { # There was an error, abort the loop break; } } ?>