PHP Regular expressions to determine the browser language instance

Source: Internet
Author: User

PHP function instance, based on the regular expression to judge the browser's default language, in fact, PHP Judge browser language using Super global variable _server["http_accept_language"] can be obtained, Then according to the HTTP request Accept-language: header information with a regular match, the final judgment of the language type.

PHP to determine the browser instance function:

01<?php

02$lang=substr ($_server[' http_accept_language '],0,4);

03//only the first 4 digits to determine the most preferred language

04if (Preg_match ("/zh-c/i", $lang))

echo "Simplified Chinese";

06else if (Preg_match ("/zh/i", $lang))

echo "Chinese";

08else if (Preg_match ("/en/i", $lang))

echo "中文版";

10else if (Preg_match ("/fr/i", $lang))

One echo "French";

12else if (Preg_match ("/de/i", $lang))

echo "German";

14else if (Preg_match ("/jp/i", $lang))

echo "Japanese";

16else if (Preg_match ("/ko/i", $lang))

echo "Korean";

18else if (Preg_match ("/es/i", $lang))

echo "Spanish";

20else if (Preg_match ("/sv/i", $lang))

echo "Swedish";

22else

echo $_server["Http_accept_language"];

24?>

This function automatically returns the result of the call when it is used only by calling a function name.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.