PHP function example, based on regular expressions to determine the browser's default language. In fact, PHP can determine the browser language by using the super global variable _SERVER["HTTP_ACCEPT_LANGUAGE"], and then based on the HTTP request Accept-Language: The header information is matched using regular expressions, and the language type is finally determined.
PHP determines browser instance function:
01
02$lang=substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,4);
03//Only take the first 4 digits to determine the highest priority language
04if (preg_match("/zh-c/i", $lang))
05 echo "Simplified Chinese";
06else if (preg_match("/zh/i", $lang))
07 echo "Traditional Chinese";
08else if (preg_match("/en/i", $lang))
09 echo "English";
10else if (preg_match("/fr/i", $lang))
11 echo "French";
12else if (preg_match("/de/i", $lang))
13 echo "German";
14else if (preg_match("/jp/i", $lang))
15 echo "Japanese";
16else if (preg_match("/ko/i", $lang))
17 echo "Korean";
18else if (preg_match("/es/i", $lang))
19 echo "Spanish";
20else if (preg_match("/sv/i", $lang))
21 echo "Swedish";
22else
23 echo $_SERVER["HTTP_ACCEPT_LANGUAGE"];
24?>
When using it, you only need to call the function name, and this function will automatically return the detection result.