Home  >  Article  >  Backend Development  >  PHP regular expression to determine browser language example_PHP tutorial

PHP regular expression to determine browser language example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:191011browse

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/765432.htmlTechArticlePHP function example, based on regular expressions to determine the default language of the browser, in fact, PHP determines the browser language using super Global variable _SERVER[HTTP_ACCEPT_LANGUAGE] can be obtained, then...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn