note 52786 deleted from reserved.variables by felipe

[email protected]
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: www dot php dot net at webdevelopers dot cz 

----

Simple function that selects "best" language for the user from the list of available languages:

function chooseLang($availableLangs) {
	$pref=array();
	foreach(split(',', $_SERVER["HTTP_ACCEPT_LANGUAGE"]) as $lang) {
		if (preg_match('/^([a-z]+).*?(?:;q=([0-9.]+))?/i', $lang.';q=1.0', $split)) {
			$pref[sprintf("%f%d", $split[2], rand(0,9999))]=strtolower($split[1]);		
		}
	}
	krsort($pref);
	return array_shift(array_merge(array_intersect($pref, $availableLangs), $availableLangs));
}
 
echo 'BESTLANG: '.chooseLang(array('cs', 'sk', 'ru', 'en'));

Daniel "elixon" Sevcik
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.