phphttp-accept-language

Check if array value contains browser country code


I'm trying to show a link to a German version of my landing page if German is one of the browser languages, but the issue I am having is that the array is returning all languages in one value. How can I check to see if the value contains certain letters? Here is the code I am using and the return I see:

$langarray = array(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'],0,100));
if(in_array('de', $langarray)){
    $showdelink = '<a style="color: #004f5d;" href="/de/">View the DE homepage</a>';
}

I am getting the following returned:

Array ( [0] => en-US,en;q=0.9,de;q=0.8 )

So the issue I am having is the value is not specifically "de", but does contain "de", so the link is not displaying. How can I check that "de" is withing the returned value. Thank you


Solution

  • you could use strpos, as

    if(strpos($langarray[0], "de") !== false) {
        $showdelink = '<a style="color: #004f5d;" href="/de/">View the DE homepage</a>';
    }