User agent, extract OS and browser from string

前端 未结 3 1762
难免孤独
难免孤独 2021-01-13 09:48

I\'d like to extract full the OS name and browser from the user agent string. How can I do this?

相关标签:
3条回答
  • 2021-01-13 10:00

    There's also a built-in function in PHP to achieve this and more: get_browser().

    $agent = get_browser();
    echo $agent->platform;
    echo $agent->parent; // or $agent->browser . $agent->version
    
    0 讨论(0)
  • 2021-01-13 10:11

    @augustknight: Note, the IE11 doesn´t send a "MSIE" token, i would suggest to add a match to a Trident token.

    IE 11 user agent sample:

    Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko
    

    change code:

    <?php
    public static function getUserAgent()
    {
        static $agent = null;
    
        if ( empty($agent) ) {
            $agent = $_SERVER['HTTP_USER_AGENT'];
    
            if ( stripos($agent, 'Firefox') !== false ) {
                $agent = 'firefox';
            } elseif ( stripos($agent, 'MSIE') !== false ) {
                $agent = 'ie';
            } elseif ( stripos($agent, 'Trident') !== false ) {
                $agent = 'ie';
            } elseif ( stripos($agent, 'iPad') !== false ) {
                $agent = 'ipad';
            } elseif ( stripos($agent, 'Android') !== false ) {
                $agent = 'android';
            } elseif ( stripos($agent, 'Chrome') !== false ) {
                $agent = 'chrome';
            } elseif ( stripos($agent, 'Safari') !== false ) {
                $agent = 'safari';
            } elseif ( stripos($agent, 'AIR') !== false ) {
                $agent = 'air';
            } elseif ( stripos($agent, 'Fluid') !== false ) {
                $agent = 'fluid';
            }
    
        }
    
        return $agent;
    }
    

    ?>

    0 讨论(0)
  • 2021-01-13 10:19

    I think it is tricky to get the full OS name and full browser name since many browsers identify themselves differently. You will probably need some fancy regex and then it might not even work 100% of the time.

    Here is the simple method I use to identify the browser. You may be able to adapt it to suit your needs.

    <?php
    
    public static function getUserAgent()
    {
        static $agent = null;
    
        if ( empty($agent) ) {
            $agent = $_SERVER['HTTP_USER_AGENT'];
    
            if ( stripos($agent, 'Firefox') !== false ) {
                $agent = 'firefox';
            } elseif ( stripos($agent, 'MSIE') !== false ) {
                $agent = 'ie';
            } elseif ( stripos($agent, 'iPad') !== false ) {
                $agent = 'ipad';
            } elseif ( stripos($agent, 'Android') !== false ) {
                $agent = 'android';
            } elseif ( stripos($agent, 'Chrome') !== false ) {
                $agent = 'chrome';
            } elseif ( stripos($agent, 'Safari') !== false ) {
                $agent = 'safari';
            } elseif ( stripos($agent, 'AIR') !== false ) {
                $agent = 'air';
            } elseif ( stripos($agent, 'Fluid') !== false ) {
                $agent = 'fluid';
            }
    
        }
    
        return $agent;
    }
    
    0 讨论(0)
提交回复
热议问题