PHP Detect Browser Language

Detect Browser Language with PHP

Need to know your visitor’s language? Detecting a browser’s language setting can quickly be accomplished with one line of PHP.

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:

And the results would look something like the following:

en-GB, sv;q=0.7, en;q=0.9

 

Leave a Reply

Your email address will not be published. Required fields are marked *