本文介绍了不推荐使用PHP eregi()检测浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有检测带有版本的浏览器的功能:

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

我需要用preg_match()函数替换eregi()函数.因为PHP 5.3中不推荐使用eregi().

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

但是,如果我用pregmatch()替换此函数,则会出现很多错误: 警告:preg_match():未知修饰符'|'....

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 '('....

我该如何解决?

推荐答案

使用相同的定界符替换开头和结尾的每个模式:

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)/',

以此类推...

这篇关于不推荐使用PHP eregi()检测浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 00:56