I've been given a script to work with for detecting browsers, to apply the correct css stylesheet. It's a little above my head, and I'm trying to determine how to make edits, to add in an if statement for this:

Mozilla/5.0 (Linux; Android 4.2.2; Nexus 7 Build/JDQ39) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.123 Safari/537.22

Right now, its defaulting to the final else statement for gecko. I want to create a statement that redirects android tablet users. Dealing with these user agents is an entirely new thing for me.

I tried adding a simple statement like this:

else if(strstr(css_browser_selector(),"Android") == true && strstr(css_browser_selector(),"Chrome") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/test.css')?>" />
        <?php echo "test";
    }

But it still, jumps to the last else statement.

<?php
    $iphone_test = strstr(css_browser_selector(),"iphone");
    $ipad_test = strstr(css_browser_selector(),"ipad");

    if($iphone_test == true || $ipad_test == true)
    {
        $mobile_active = true;
    }
    else
    {
        $mobile_active = false;
    }

    $ie9_test = strstr(css_browser_selector(),"ie9");
    $ie10_test = strstr(css_browser_selector(),"ie10");

    if($ie9_test == true || $ie10_test == true)
    {
        $ieX_active = true;
    }
    else
    {
        $ieX_active = false;
    }

    if(strstr(css_browser_selector(),"webkit") == true && $mobile_active == false && strstr(css_browser_selector(),"chrome") == false){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/safari.css') ?>" />
        <?php
    }
    else if(strstr(css_browser_selector(),"iphone") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/iphone.css')?>" />
        <?php
    }
    else if(strstr(css_browser_selector(),"ipad") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/ipad.css')?>" />
        <?php
    }
    else if(strstr(css_browser_selector(),"chrome") == true && strstr(css_browser_selector(),"win") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/chrome.css')?>" />
        <?php
    }
    else if(strstr(css_browser_selector(),"ie") == true && $ieX_active == false){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/ie.css')?>" />
        <?php
    }
    else if($ieX_active == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/ieX.css')?>" />
        <?php
    }
    else
    {
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/gecko.css')?>" />
        <?php echo "test";
    }
 ?>

If needed, this script is included also.

<?php

/*
PHP CSS Browser Selector v0.0.1
Bastian Allgeier (http://bastian-allgeier.de)
http://bastian-allgeier.de/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Credits: This is a php port from Rafael Lima's original Javascript CSS Browser Selector: http://rafael.adm.br/css_browser_selector
*/

function css_browser_selector($ua=null) {
        $ua = ($ua) ? strtolower($ua) : strtolower($_SERVER['HTTP_USER_AGENT']);        

        $g = 'gecko';
        $w = 'webkit';
        $s = 'safari';
        $b = array();

        // browser
        if(!preg_match('/opera|webtv/i', $ua) && preg_match('/msie\s(\d)/', $ua, $array)) {
                $b[] = 'ie ie' . $array[1];
        }   else if(strstr($ua, 'firefox/2')) {
                $b[] = $g . ' ff2';     
        }   else if(strstr($ua, 'firefox/3.5')) {
                $b[] = $g . ' ff3 ff3_5';
        }   else if(strstr($ua, 'firefox/3')) {
                $b[] = $g . ' ff3';
        } else if(strstr($ua, 'gecko/')) {
                $b[] = $g;
        } else if(preg_match('/opera(\s|\/)(\d+)/', $ua, $array)) {
                $b[] = 'opera opera' . $array[2];
        } else if(strstr($ua, 'konqueror')) {
                $b[] = 'konqueror';
        } else if(strstr($ua, 'chrome')) {
                $b[] = $w . ' ' . $s . ' chrome';
        } else if(strstr($ua, 'iron')) {
                $b[] = $w . ' ' . $s . ' iron';
        } else if(strstr($ua, 'applewebkit/')) {
                $b[] = (preg_match('/version\/(\d+)/i', $ua, $array)) ? $w . ' ' . $s . ' ' . $s . $array[1] : $w . ' ' . $s;
        } else if(strstr($ua, 'mozilla/')) {
                $b[] = $g;
        }

        // platform             
        if(strstr($ua, 'j2me')) {
                $b[] = 'mobile';
        } else if(strstr($ua, 'iphone')) {
                $b[] = 'iphone';        
        } else if(strstr($ua, 'ipad')) {
                $b[] = 'ipad';      
        } else if(strstr($ua, 'ipod')) {
                $b[] = 'ipod';      
        } else if(strstr($ua, 'mac')) {
                $b[] = 'mac';       
        } else if(strstr($ua, 'darwin')) {
                $b[] = 'mac';       
        } else if(strstr($ua, 'webtv')) {
                $b[] = 'webtv';     
        } else if(strstr($ua, 'win')) {
                $b[] = 'win';       
        } else if(strstr($ua, 'freebsd')) {
                $b[] = 'freebsd';       
        } else if(strstr($ua, 'x11') || strstr($ua, 'linux')) {
                $b[] = 'linux';     
        }

        return join(' ', $b);

}

?>

You can try adding the following after the closing brace at line 42:

else if(strstr($ua, 'android') && strstr($ua, 'mobile')) { // check if android mobile
        $b[] = 'android mobile';
} else if(strstr($ua, 'android') && !strstr($ua, 'mobile')) { // check if android tablet
        $b[] = 'android tablet';
}

A couple of things strike me as curious about the scripts. According to the PHP docs, strpos() should be used when simply determining whether a string occurs in another, as it's faster than strstr(). I'm not sure
why the browser selector script uses strstr() instead.
Second, your first script calls css_browser_selector() over and over again. This could be taxing, if you run multiple strstr() calls over and over. Why not just call css_browser_selector() once at the beginning of the script, assign it to a variable, and then test against that string as needed?

hmmmm...

Doesn't seem to be any change? Hopefully I understood the first part right. I haven't gotten to your other suggestions yet regarding the multiple function calls.

I added in your code (with a minor change of setting android mobile/tablet as variables) and this is where I stand now, with the browser still defaulting to my last else statement. currently testing on an android phone.

I have:

function css_browser_selector($ua=null) {
        $ua = ($ua) ? strtolower($ua) : strtolower($_SERVER['HTTP_USER_AGENT']);        

        $g = 'gecko';
        $w = 'webkit';
        $s = 'safari';
        $a_m = 'android mobile';
        $a_t = 'android tablet';
        $b = array();


        // browser
        if(!preg_match('/opera|webtv/i', $ua) && preg_match('/msie\s(\d)/', $ua, $array)) {
                $b[] = 'ie ie' . $array[1];
        }   else if(strstr($ua, 'firefox/2')) {
                $b[] = $g . ' ff2';     
        }   else if(strstr($ua, 'firefox/3.5')) {
                $b[] = $g . ' ff3 ff3_5';
        }   else if(strstr($ua, 'firefox/3')) {
                $b[] = $g . ' ff3';
        } else if(strstr($ua, 'gecko/')) {
                $b[] = $g;
        } else if(preg_match('/opera(\s|\/)(\d+)/', $ua, $array)) {
                $b[] = 'opera opera' . $array[2];
        } else if(strstr($ua, 'konqueror')) {
                $b[] = 'konqueror';
        } else if(strstr($ua, 'chrome')) {
                $b[] = $w . ' ' . $s . ' chrome';
        } else if(strstr($ua, 'iron')) {
                $b[] = $w . ' ' . $s . ' iron';
        } else if(strstr($ua, 'applewebkit/')) {
                $b[] = (preg_match('/version\/(\d+)/i', $ua, $array)) ? $w . ' ' . $s . ' ' . $s . $array[1] : $w . ' ' . $s;
        } else if(strstr($ua, 'mozilla/')) {
                $b[] = $g;
        } else if(strstr($ua, 'Android') && strstr($ua, 'Mobile')) { // check if android mobile
                $b[] = $a_m;
        } else if(strstr($ua, 'Android') && !strstr($ua, 'Mobile')) { // check if android tablet
                $b[] = $a_t;
        }
// etc. the rest is the same.

and in the first script I added in:

else if(strpos(css_browser_selector(),"android mobile") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/nest_test.css')?>" />
        <?php echo "test";
    }

It seems to make more sense with just basic css links based on screen sizes, etc, but I think I've got it doing what I need with this:

<?php
    $iphone_test = strstr(css_browser_selector(),"iphone");
    $ipad_test = strstr(css_browser_selector(),"ipad");


    if($iphone_test == true || $ipad_test == true)
    {
        $mobile_active = true;
    }
    else
    {
        $mobile_active = false;
    }

    $ie9_test = strstr(css_browser_selector(),"ie9");
    $ie10_test = strstr(css_browser_selector(),"ie10");

    if($ie9_test == true || $ie10_test == true)
    {
        $ieX_active = true;
    }
    else
    {
        $ieX_active = false;
    }

    if(strstr(css_browser_selector(),"webkit") == true && $mobile_active == false && strstr(css_browser_selector(),"chrome") == false){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/safari.css') ?>" />
        <?php  // safari appears fine here.
    }
    else if(strstr(css_browser_selector(),"iphone") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/iphone.css')?>" />
        <?php
    }
    else if(strstr(css_browser_selector(),"ipad") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/ipad.css')?>" />
        <?php
    }
    else if(strstr(css_browser_selector(),"chrome") == true && strstr(css_browser_selector(),"win") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/chrome.css')?>" />
        <?php 
    }
    else if(strstr(css_browser_selector(),"mozilla") == true && strstr(css_browser_selector(),"win") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/chrome.css')?>" />
        <?php 
    }
    else if(strstr(css_browser_selector(),"gecko") == true && strstr(css_browser_selector(),"android") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/chrome.css')?>" />
        <?php 
    }
    else if(strstr(css_browser_selector(),"android") == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/android.css')?>" />
        <?php 
    }
    else if(strstr(css_browser_selector(),"ie") == true && $ieX_active == false){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/ie.css')?>" />
        <?php
    }
    else if($ieX_active == true){
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/nest_ieX.css')?>" />
        <?php
    }
    else
    {
        ?>
        <link rel="stylesheet" media="screen" type="text/css" href="<?php echo ('css/gecko.css')?>" />
        <?php 
    }
 ?>

and for the second script, this:

<?php

/*
PHP CSS Browser Selector v0.0.1
Bastian Allgeier (http://bastian-allgeier.de)
http://bastian-allgeier.de/css_browser_selector
License: http://creativecommons.org/licenses/by/2.5/
Credits: This is a php port from Rafael Lima's original Javascript CSS Browser Selector: http://rafael.adm.br/css_browser_selector
*/

function css_browser_selector($ua=null) {
        $ua = ($ua) ? strtolower($ua) : strtolower($_SERVER['HTTP_USER_AGENT']);        

        $g = 'gecko';
        $w = 'webkit';
        $s = 'safari';
        $b = array();

        // browser
        if(!preg_match('/opera|webtv/i', $ua) && preg_match('/msie\s(\d)/', $ua, $array)) {
                $b[] = 'ie ie' . $array[1];
        }   else if(strstr($ua, 'firefox/2')) {
                $b[] = $g . ' ff2';     
        }   else if(strstr($ua, 'firefox/3.5')) {
                $b[] = $g . ' ff3 ff3_5';
        }   else if(strstr($ua, 'firefox/3')) {
                $b[] = $g . ' ff3';
        } else if(strstr($ua, 'gecko/')) {
                $b[] = $g;
        } else if(preg_match('/opera(\s|\/)(\d+)/', $ua, $array)) {
                $b[] = 'opera opera' . $array[2];
        } else if(strstr($ua, 'konqueror')) {
                $b[] = 'konqueror';
        } else if(strstr($ua, 'chrome')) {
                $b[] = $w . ' ' . $s . ' chrome';
        } else if(strstr($ua, 'iron')) {
                $b[] = $w . ' ' . $s . ' iron';
        } else if(strstr($ua, 'applewebkit/')) {
                $b[] = (preg_match('/version\/(\d+)/i', $ua, $array)) ? $w . ' ' . $s . ' ' . $s . $array[1] : $w . ' ' . $s;
        } else if(strstr($ua, 'mozilla/')) {
                $b[] = $g;
        }

        // platform             
        if(strstr($ua, 'j2me')) {
                $b[] = 'mobile';
        } else if(strstr($ua, 'iphone')) {
                $b[] = 'iphone';        
        } else if(strstr($ua, 'ipad')) {
                $b[] = 'ipad';      
        } else if(strstr($ua, 'ipod')) {
                $b[] = 'ipod';      
        } else if(strstr($ua, 'mac')) {
                $b[] = 'mac';       
        } else if(strstr($ua, 'darwin')) {
                $b[] = 'mac';       
        } else if(strstr($ua, 'webtv')) {
                $b[] = 'webtv';     
        } else if(strstr($ua, 'win')) {
                $b[] = 'win';       
        } else if(strstr($ua, 'freebsd')) {
                $b[] = 'freebsd';       
        } else if(strstr($ua, 'x11') || strstr($ua, 'linux') && !strstr($ua, 'android')) {
                $b[] = 'linux';     
        } else if(strstr($ua, 'android')) {
                $b[] = 'android';       
        } else if(strstr($ua, 'android') && strstr($ua, 'linux')) {
                $b[] = 'android';       
        }

        return join(' ', $b);

}

?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.