gpt4 book ai didi

PHP:浏览器版本号 user-agent with Version/x.x.x (Safari & Opera)

转载 作者:可可西里 更新时间:2023-11-01 12:43:42 28 4
gpt4 key购买 nike

我编写了一个简单的类来检查用户代理是否会针对不兼容的浏览器显示警告。我正在做这个服务器端,我知道它可能是客户端。

好的,首先,我不太擅长编写正则表达式..

我写了一个正则表达式来搜索小写浏览器名称后跟版本号。我做了一个 foreach()用数组这样的东西:

<?php
$browsers = Array('msie','chrome','safari','firefox','opera');

foreach($browsers as $i => $browser)
{
$regex = "#({$browser})[/ ]([0-9.]*)#i";

if(preg_match($regex, $useragent, $matches))
{
echo "Browser: \"{$matches[0]}\", version: \"{$matches[1]}\"";
}
}
?>

这将产生:Browser: "Firefox", version "23.0.6" .

我发现它适用于 Firefox、MS IE 和旧版本的 Opera。然而,某些浏览器(如 Safari 和较新版本的 Opera)具有不同的用户代理字符串,其中包含 Version/x.x.x,即

只是为了让您了解这里有 3 个用户代理字符串,我需要的已突出显示。

  1. Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/536.30.1 (KHTML, like Gecko) Version/<strong><em>6.0.5</em></strong> <em><strong>Safari</strong></em>/536.30.1
  2. Mozilla/5.0 (compatible; <strong><em>MSIE 10.0</em></strong>; Windows NT 6.1; WOW64; Trident/6.0)
  3. <strong><em>Opera</em></strong>/9.80 (Windows NT 6.0) Presto/2.12.388 Version/<strong><em>12.14</em></strong>

因此在以下每一个中,以下人类逻辑都是正确的:

  • 如果有Version/<strong><em>x.x.x</em></strong>在版本号的字符串中。
  • 如果没有则Browsername/<strong><em>x.x.x</em></strong>是版本号。

此外,如果您查看上面的第一个也是最后一个用户代理字符串,您可以看到 Version可以出现在浏览器名称之后的 之前。

有人可以帮我制作一个与preg_match() 一起使用的正则表达式吗? ?我需要使用条件语句还是可以搜索可选分组?我有点困惑..

谢谢!

编辑 17-09-2013: 我忘了说,我不想使用 get_browser() ,它使用一个巨大的库来检测浏览器的功能等。我只需要一个简短的浏览器“白名单”,它应该花费几毫秒而不是几百毫秒来读取浏览器 cap.ini 文件。否则乔治的回答本来就是的答案..

最佳答案

由于 Remus 的回答我在某些浏览器上遇到了一些问题,所以最终做了一些不同的事情。

<?php

function get_useragent_info($ua)
{
$ua = is_null($ua) ? $_SERVER['HTTP_USER_AGENT'] : $ua;
// Enumerate all common platforms, this is usually placed in braces (order is important! First come first serve..)
$platforms = "Windows|iPad|iPhone|Macintosh|Android|BlackBerry";

// All browsers except MSIE/Trident and..
// NOT for browsers that use this syntax: Version/0.xx Browsername
$browsers = "Firefox|Chrome";

// Specifically for browsers that use this syntax: Version/0.xx Browername
$browsers_v = "Safari|Mobile"; // Mobile is mentioned in Android and BlackBerry UA's

// Fill in your most common engines..
$engines = "Gecko|Trident|Webkit|Presto";

// Regex the crap out of the user agent, making multiple selections and..
$regex_pat = "/((Mozilla)\/[\d\.]+|(Opera)\/[\d\.]+)\s\(.*?((MSIE)\s([\d\.]+).*?(Windows)|({$platforms})).*?\s.*?({$engines})[\/\s]+[\d\.]+(\;\srv\:([\d\.]+)|.*?).*?(Version[\/\s]([\d\.]+)(.*?({$browsers_v})|$)|(({$browsers})[\/\s]+([\d\.]+))|$).*/i";

// .. placing them in this order, delimited by |
$replace_pat = '$7$8|$2$3|$9|${17}${15}$5$3|${18}${13}$6${11}';

// Run the preg_replace .. and explode on |
$ua_array = explode("|",preg_replace($regex_pat, $replace_pat, $ua, PREG_PATTERN_ORDER));

if (count($ua_array)>1)
{
$return['platform'] = $ua_array[0]; // Windows / iPad / MacOS / BlackBerry
$return['type'] = $ua_array[1]; // Mozilla / Opera etc.
$return['renderer'] = $ua_array[2]; // WebKit / Presto / Trident / Gecko etc.
$return['browser'] = $ua_array[3]; // Chrome / Safari / MSIE / Firefox

/*
Not necessary but this will filter out Chromes ridiculously long version
numbers 31.0.1234.122 becomes 31.0, while a "normal" 3 digit version number
like 10.2.1 would stay 10.2.1, 11.0 stays 11.0. Non-match stays what it is.
*/

if (preg_match("/^[\d]+\.[\d]+(?:\.[\d]{0,2}$)?/",$ua_array[4],$matches))
{
$return['version'] = $matches[0];
}
else
{
$return['version'] = $ua_array[4];
}
}
else
{
/*
Unknown browser..
This could be a deal breaker for you but I use this to actually keep old
browsers out of my application, users are told to download a compatible
browser (99% of modern browsers are compatible. You can also ignore my error
but then there is no guarantee that the application will work and thus
no need to report debugging data.
*/

return false;
}

// Replace some browsernames e.g. MSIE -> Internet Explorer
switch(strtolower($return['browser']))
{
case "msie":
case "trident":
$return['browser'] = "Internet Explorer";
break;
case "": // IE 11 is a steamy turd (thanks Microsoft...)
if (strtolower($return['renderer']) == "trident")
{
$return['browser'] = "Internet Explorer";
}
break;
}

switch(strtolower($return['platform']))
{
case "android": // These browsers claim to be Safari but are BB Mobile
case "blackberry": // and Android Mobile
if ($return['browser'] =="Safari" || $return['browser'] == "Mobile" || $return['browser'] == "")
{
$return['browser'] = "{$return['platform']} mobile";
}
break;
}

return $return;
} // End of Function
?>

关于PHP:浏览器版本号 user-agent with Version/x.x.x (Safari & Opera),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18818362/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com