PHP eregi()已被弃用检测浏览器[重复](PHP eregi() is deprecated

2019-10-19 05:04发布

这个问题已经在这里有一个答案:

  • 我如何转换额日格表达式在PHP中浸渍料? 4个回答

我有一个函数来检测浏览器的版本:

    # 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';
    }

我需要替换的preg_match()函数eregi()函数。 因为eregi()在PHP 5.3已弃用。

但是,如果我更换pregmatch此功能()我得到很多的错误:警告:的preg_match():未知的修饰词“|” ....

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

我怎样才能解决这个问题?

Answer 1:

在开始更换每一个图案和相同的分隔符符号结束:

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

等等...



Answer 2:

需要添加改性剂

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


文章来源: PHP eregi() is deprecated detect browser [duplicate]