php 解析用户代理字符串的代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2122786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Code to parse user agent string?
提问by Spot
As strange as I find this, I have not been able to find a good PHP function anywhere which will do an intelligent parse of a user agent string? Googled it for about 20 minutes now.
就像我发现的那样奇怪,我无法在任何地方找到一个好的 PHP 函数来对用户代理字符串进行智能解析?现在用谷歌搜索了大约 20 分钟。
I have the string already, I just need something that will chop it up and give me at least browser/ver/os.
我已经有了字符串,我只需要一些可以将其切碎并至少给我浏览器/版本/操作系统的东西。
Know of a good snippet anywhere?
知道一个好的片段吗?
采纳答案by symcbean
The get_browser()function has been available in PHP for quite a long a time.
该get_browser()函数在 PHP 中已经可用很长时间了。
The PHP manual is free, can be downloaded in various formats and viewed online (with comments)
PHP 手册是免费的,可以以各种格式下载并在线查看(附评论)
回答by Martin Vseticka
https://github.com/browscap/browscap-php- this is a standalone library that aims to replace get_browserfunction. For shared hostings it seems to be a better option.
https://github.com/browscap/browscap-php- 这是一个旨在替换get_browser功能的独立库。对于共享主机,它似乎是一个更好的选择。
Works on: PHP 5
适用于:PHP 5
回答by Piotr Olaszewski
You can try to use: https://github.com/tobie/ua-parser. This is multi-language tool to parsing user agent string. For PHP is here: https://github.com/tobie/ua-parser/tree/master/php
您可以尝试使用:https: //github.com/tobie/ua-parser。这是解析用户代理字符串的多语言工具。PHP 在这里:https: //github.com/tobie/ua-parser/tree/master/php
回答by ThaDafinser
Try out ThaDafinser/UserAgentParserit's an abstraction for many available user agent parsers.
试试ThaDafinser/UserAgentParser它是许多可用用户代理解析器的抽象。
So if you are not happy with one - just switch to another (or combine them)
因此,如果您对其中一个不满意 - 只需切换到另一个(或将它们组合)
You can try it out here
你可以在这里试试
回答by gthuo
There is a PHP class library i found out, and it has worked so well for me. It is in the link below.
我发现了一个 PHP 类库,它对我来说效果很好。它在下面的链接中。
https://www.toms-world.org/blog/parseuseragentstring
https://www.toms-world.org/blog/parseuseragentstring
It is lightweight and just does what I wanted (Browser, Version, OS, Mobile/PC, and much more) in a memory-friendy manner.
它是轻量级的,并且以内存友好的方式执行我想要的操作(浏览器、版本、操作系统、移动/PC 等等)。

