往wordpress评论useragent图标添加win8.1的支持

2013.11.04 分享 13177 人浏览 7 条留言

前不久往我的评论区域添加了useragent图标,代码来自牧风提取插件的精简版,然后最近又把我的老爷机升级到win8.1了,然后在自己博客的评论区域中发现我的系统图标是个问号,看得很不爽,就动动手往里面添加一个win8.1吧。

usergent.php代码如下

  1. <?php  
  2. /** Get Browser Infomations */  
  3. function get_browsers($ua){  
  4.     $title = 'unknow';  
  5.     $icon = 'unknow';     
  6.     if (preg_match('#MSIE ([a-zA-Z0-9.]+)#i', $ua$matches)) {  
  7.         $title = 'Internet Explorer '. $matches[1];  
  8.         if ( strpos($matches[1], '7') !== false || strpos($matches[1], '8') !== false)  
  9.             $icon = 'ie8';  
  10.         elseif ( strpos($matches[1], '9') !== false)  
  11.             $icon = 'ie9';  
  12.         elseif ( strpos($matches[1], '10') !== false)  
  13.             $icon = 'ie10';  
  14.         else  
  15.             $icon = 'ie';  
  16.     }elseif (preg_match('#Firefox/([a-zA-Z0-9.]+)#i', $ua$matches)){  
  17.         $title = 'Firefox '. $matches[1];  
  18.         $icon = 'firefox';  
  19.     }elseif (preg_match('#CriOS/([a-zA-Z0-9.]+)#i', $ua$matches)){  
  20.         $title = 'Chrome for iOS '. $matches[1];  
  21.         $icon = 'crios';  
  22.     }elseif (preg_match('#Chrome/([a-zA-Z0-9.]+)#i', $ua$matches)) {  
  23.         $title = 'Google Chrome '. $matches[1];  
  24.         $icon = 'chrome';  
  25.         if (preg_match('#OPR/([a-zA-Z0-9.]+)#i', $ua$matches)) {  
  26.             $title = 'Opera '. $matches[1];  
  27.             $icon = 'opera15';  
  28.             if (preg_match('#opera mini#i', $ua)) $title = 'Opera Mini'. $matches[1];  
  29.         }  
  30.     }elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i', $ua$matches)) {  
  31.         $title = 'Safari '. $matches[1];  
  32.         $icon = 'safari';  
  33.     }elseif (preg_match('#Opera.(.*)Version[ /]([a-zA-Z0-9.]+)#i', $ua$matches)) {  
  34.         $title = 'Opera '. $matches[2];  
  35.         $icon = 'opera';  
  36.         if (preg_match('#opera mini#i', $ua)) $title = 'Opera Mini'. $matches[2];         
  37.     }elseif (preg_match('#Maxthon( |\/)([a-zA-Z0-9.]+)#i', $ua,$matches)) {  
  38.         $title = 'Maxthon '. $matches[2];  
  39.         $icon = 'maxthon';  
  40.     }elseif (preg_match('#360([a-zA-Z0-9.]+)#i', $ua$matches)) {  
  41.         $title = '360 Browser '. $matches[1];  
  42.         $icon = '360se';  
  43.     }elseif (preg_match('#SE 2([a-zA-Z0-9.]+)#i', $ua$matches)) {  
  44.         $title = 'SouGou Browser 2'.$matches[1];  
  45.         $icon = 'sogou';  
  46.     }elseif (preg_match('#UCWEB([a-zA-Z0-9.]+)#i', $ua$matches)) {  
  47.         $title = 'UCWEB '. $matches[1];  
  48.         $icon = 'ucweb';  
  49.     }elseif(preg_match('#wp-(iphone|android)/([a-zA-Z0-9.]+)#i', $ua$matches)){   
  50.         $title = 'wordpress '. $matches[2];  
  51.         $icon = 'wordpress';  
  52.     }  
  53.     return array(  
  54.         $title,  
  55.         $icon  
  56.     );  
  57. }  
  58. function get_os($ua){  
  59.     $title = 'unknow';  
  60.     $icon = 'unknow';  
  61.     if (preg_match('/win/i', $ua)) {  
  62.         if (preg_match('/Windows NT 6.1/i', $ua)) {  
  63.             $title = "Windows 7";  
  64.             $icon = "windows_win7";  
  65.         }elseif (preg_match('/Windows NT 5.1/i', $ua)) {  
  66.             $title = "Windows XP";  
  67.             $icon = "windows";  
  68.         }elseif (preg_match('/Windows NT 6.2/i', $ua)) {  
  69.             $title = "Windows 8";  
  70.             $icon = "windows_win8";  
  71.         }elseif (preg_match('/Windows NT 6.3/i', $ua)) {// 2013.11.4 添加win8.1支持 https://huilang.me   
  72.             $title = "Windows 8.1";  
  73.             $icon = "windows_win8.1";  
  74.         }elseif (preg_match('/Windows NT 6.0/i', $ua)) {  
  75.             $title = "Windows Vista";  
  76.             $icon = "windows_vista";  
  77.         }elseif (preg_match('/Windows NT 5.2/i', $ua)) {  
  78.             if (preg_match('/Win64/i', $ua)) {  
  79.                 $title = "Windows XP 64 bit";  
  80.             } else {  
  81.                 $title = "Windows Server 2003";  
  82.             }  
  83.             $icon = 'windows';  
  84.         }elseif (preg_match('/Windows Phone/i', $ua)) {  
  85.             $matches = explode(';',$ua);  
  86.             $title = $matches[2];  
  87.             $icon = "windows_phone";  
  88.         }  
  89.     }elseif (preg_match('#iPod.*.CPU.([a-zA-Z0-9.( _)]+)#i', $ua$matches)) {  
  90.         $title = "iPod ".$matches[1];  
  91.         $icon = "iphone";  
  92.     } elseif (preg_match('#iPhone OS ([a-zA-Z0-9.( _)]+)#i', $ua$matches)) {  
  93.         $title = "Iphone ".$matches[1];  
  94.         $icon = "iphone";  
  95.     } elseif (preg_match('#iPad.*.CPU.([a-zA-Z0-9.( _)]+)#i', $ua$matches)) {  
  96.         $title = "iPad ".$matches[1];  
  97.         $icon = "ipad";  
  98.     } elseif (preg_match('/Mac OS X.([0-9. _]+)/i', $ua$matches)) {  
  99.         if(count(explode(7,$matches[1]))>1) $matches[1] = 'Lion '.$matches[1];  
  100.         elseif(count(explode(8,$matches[1]))>1) $matches[1] = 'Mountain Lion '.$matches[1];  
  101.         $title = "Mac OSX ".$matches[1];  
  102.         $icon = "macos";  
  103.     } elseif (preg_match('/Macintosh/i', $ua)) {  
  104.         $title = "Mac OS";  
  105.         $icon = "macos";  
  106.     } elseif (preg_match('/CrOS/i', $ua)){  
  107.         $title = "Google Chrome OS";  
  108.         $icon = "chrome";  
  109.     }elseif (preg_match('/Linux/i', $ua)) {  
  110.         $title = 'Linux';  
  111.         $icon = 'linux';  
  112.         if (preg_match('/Android.([0-9. _]+)/i',$ua$matches)) {  
  113.             $title$matches[0];  
  114.             $icon = "android";  
  115.         }elseif (preg_match('#Ubuntu#i', $ua)) {  
  116.             $title = "Ubuntu Linux";  
  117.             $icon = "ubuntu";  
  118.         }elseif(preg_match('#Debian#i', $ua)) {  
  119.             $title = "Debian GNU/Linux";  
  120.             $icon = "debian";  
  121.         }elseif (preg_match('#Fedora#i', $ua)) {  
  122.             $title = "Fedora Linux";  
  123.             $icon = "fedora";  
  124.         }  
  125.     }  
  126.     return array(  
  127.         $title,  
  128.         $icon  
  129.     );  
  130. }  
  131. function get_useragent($ua){  
  132.     $url = get_bloginfo('template_directory') . '/img/browsers/';  
  133.     $browser = get_browsers($ua);  
  134.     $os = get_os($ua);  
  135.     echo '<img src="'.$url.$browser[1].'.png" title="'.$browser[0].'" class="useragent" alt="'.$browser[0].'"><img src="'.$url.$os[1].'.png" title="'.$os[0].'" class="useragent" alt="'.$os[0].'">';  
  136. }  

其实操作非常简单,就是查找下win8.1的版本号还是啥的(百度得知为Windows NT 6.3),然后按照win8的形式添加一个即可。

调用

  1. <?php get_useragent($comment->comment_agent);?>  

别忘了往主题的img文件夹下放置 useragen图标 图标自己在 百度网盘 提取吧,我没心思去弄win8.1的新图标,就复制了一份win8的改名为win8.1的而已...

文章地址:https://huilang.me/add-useragent-icon-with-win8-1-in-wordpress/

“往wordpress评论useragent图标添加win8.1的支持” 有 7 条评论

  1. 诸葛小觉说道:

    这是什么…PS.最近你更新真勤快…

  2. 老虎说道:

    可惜不能显示国家。 :x

回复本文

电子邮件地址不会被公开。 必填项已用*标注

icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif