Need to know your visitor’s language? Detecting a browser’s language setting can quickly be accomplished with one line of PHP.
1 2 3 4 5 6 7 |
// this gets the Browser's Language $theLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2); // this will display the two-letter language abbreviation in a web page echo "$theLanguage"; |
The above example would display either “es”, for example for Spanish, or the two-letter abbreviation “en” for English if the browser in question was either Spanish or English respectively. If the browser language was German it would be “de”. This is due to the fact that we are using a PHP substr() substring operator to retrieve just the first two characters from the PHP response of checking the browser language settings.
If you used the above code without the substring the following output might look something like this:
1 2 3 4 5 6 7 |
// this gets the Browser's Language $theLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE']; // this will display the two-letter language abbreviation in a web page echo "$theLanguage"; |