PHP eregi() is deprecated detect browser [duplicate]

可紊 提交于 2019-12-24 11:48:01

问题


I have a function to detect browser with version:

    # Agent - Webbrowsers
    function agent_browser($http_user_agent) {

        // Browsers
        $browsers = array (
            'Android Standaard' => '(Linux)(.*)(Android)(.*)(Mobile Safari)',
            'MSIE 10' => '(MSIE 10)',
            'MSIE 9' => '(MSIE 9)',
            'MSIE 8' => '(MSIE 8)',
            'MSIE 7' => '(MSIE 7)',
            'MSIE 6' => '(MSIE 6)',
            'MSIE 5' => '(MSIE 5)',
            'Opera 12' => '(Opera 12)|(Opera/)(.*)(Version/12)',
            'Opera 11' => '(Opera 11)|(Opera/)(.*)(Version/11)',
            'Opera 10' => '(Opera 10)|(Opera/)(.*)(Version/10)',
            'Opera 9' => '(Opera 9)|(Opera/9)|(Opera/)(.*)(Version/9)',
            'Opera 8' => '(Opera 8)|(Opera/8)|(Opera/)(.*)(Version/8)',
            'Firefox 25' => '(Firefox/25)',
            'Firefox 24' => '(Firefox/24)',
            'Firefox 23' => '(Firefox/23)',
            'Firefox 22' => '(Firefox/22)',
            'Firefox 21' => '(Firefox/21)',
            'Firefox 20' => '(Firefox/20)',
            'Firefox 19' => '(Firefox/19)',
            'Firefox 18' => '(Firefox/18)',
            'Firefox 17' => '(Firefox/17)',
            'Firefox 16' => '(Firefox/16)',
            'Firefox 15' => '(Firefox/15)',
            'Firefox 14' => '(Firefox/14)',
            'Firefox 13' => '(Firefox/13)',
            'Firefox 12' => '(Firefox/12)',
            'Firefox 11' => '(Firefox/11)',
            'Firefox 10' => '(Firefox/10)',
            'Firefox 9' => '(Firefox/9)',
            'Firefox 8' => '(Firefox/8)',
            'Firefox 7' => '(Firefox/7)',
            'Firefox 6' => '(Firefox/6)',
            'Firefox 5' => '(Firefox/5)',
            'Chrome 29' => '(Chrome/29)',
            'Chrome 28' => '(Chrome/28)',
            'Chrome 27' => '(Chrome/27)',
            'Chrome 26' => '(Chrome/26)',
            'Chrome 25' => '(Chrome/25)',
            'Chrome 24' => '(Chrome/24)',
            'Chrome 23' => '(Chrome/23)',
            'Chrome 22' => '(Chrome/22)',
            'Chrome 21' => '(Chrome/21)',
            'Chrome 20' => '(Chrome/20)',
            'Chrome 19' => '(Chrome/19)',
            'Chrome 18' => '(Chrome/18)',
            'Chrome 17' => '(Chrome/17)',
            'Chrome 16' => '(Chrome/16)',
            'Chrome 15' => '(Chrome/15)',
            'Chrome 14' => '(Chrome/14)',
            'Chrome 13' => '(Chrome/13)',
            'Chrome 12' => '(Chrome/12)',
            'Chrome 11' => '(Chrome/11)',
            'Chrome 10' => '(Chrome/10)',
            'BlackBerry 7' => '(BlackBerry)(.*)(Version/7)',
            'BlackBerry 6' => '(BlackBerry)(.*)(Version/6)',
            'Nokia 8' => '(BrowserNG/8)|(NokiaBrowser/8)',
            'Nokia 7' => '(BrowserNG/7)|(NokiaBrowser/7)',
            'Safari 7' => '(Version/7)(.*)(Safari/)',
            'Safari 6' => '(Version/6)(.*)(Safari/)',
            'Safari 5' => '(Version/5)(.*)(Safari/)',
            'Safari 4' => '(Version/4)(.*)(Safari/)',
            'Safari 3' => '(Version/3)(.*)(Safari/)',
            'Safari 2' => '(Version/2)(.*)(Safari/)',
            'Safari 1' => '(Version/1)(.*)(Safari/)'
        );


        foreach($browsers as $browser => $pattern){

            if(eregi($pattern, $http_user_agent)) {
                return $browser;
            }

        }

        return 'Unknown';
    }

I need to replace the eregi() function with preg_match() function. Because eregi() is deprecated in PHP 5.3.

But if i replace this function with pregmatch() i get many errors: Warning: preg_match(): Unknown modifier '|'....

Warning: preg_match(): Unknown modifier '|'....
Warning: preg_match(): Unknown modifier '('....
Warning: preg_match(): Unknown modifier '('....

How can i fix this?


回答1:


Replace every pattern at start and end with the same delimiter sign:

        'MSIE 10' => '/(MSIE 10)/',
        'MSIE 9' => '/(MSIE 9)/',
        'MSIE 8' => '/(MSIE 8)/',
        'MSIE 7' => '/(MSIE 7)/',

and so on...




回答2:


Need add modifer

 'MSIE 10' => '#(MSIE 10)#',


来源:https://stackoverflow.com/questions/20994670/php-eregi-is-deprecated-detect-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!