比较版本号的最佳灵活方法是什么?
我正在使用一个脚本来比较已安装和可用应用程序的版本号。通常,我会使用简单的比较运算符。由于我是在 PHP 5.3 环境中构建此应用程序,因此我考虑使用 version_compare(),但这似乎并不像我希望的那样完全满足我的需求。
我正在比较的版本字符串可以遵循多种格式,但到目前为止我遇到的格式是:
- “2.6.18-164.6.1.el5”与“2.6.18-92.1.13.el5”
- “4.3p2”与“5.1” p1'
- '5.1.6' 与 '5.2.12'
- '2.6.24.4-foo.bar.x.i386' 与 '2.4.21-40'
如您所见,确实没有一致的格式供我使用。
我考虑做的一件事是将每个版本字符串拆分为非数字字符,然后迭代结果数组并比较相对索引。但是,我不确定这是否是一个好方法,特别是在“2.6.24-4-foo.a.12.i386”与“2.6.24-4-foo.b.12”的情况下.i386'。
是否有任何经过充分测试的方法可以比较非常宽松的版本号(例如,特别是在 PHP 环境中)?
I am working with a script to compare version numbers for installed and available applications. I would, on a normal basis, use simple comparison operators. Since I am building this application in a PHP 5.3 environment, I have considered the use of version_compare(), but that doesn't seem to suit my needs as cleanly as I would like.
The version strings I am comparing can follow many formats, but those I have encountered thus far are:
- '2.6.18-164.6.1.el5' versus '2.6.18-92.1.13.el5'
- '4.3p2' versus '5.1p1'
- '5.1.6' versus '5.2.12'
- '2.6.24.4-foo.bar.x.i386' versus '2.4.21-40'
As you can see, there really is no consistent format for me to work with.
The one thing I considered doing was splitting each version string on the non-numeric characters, then iterating the resulting arrays and comparing relative indices. However, I'm not sure that would be a good way of doing it, especially in the case of '2.6.24-4-foo.a.12.i386' versus '2.6.24-4-foo.b.12.i386'.
Are there any well-tested methods of comparing very loose version numbers such as this, specifically in a PHP environment?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按符号拆分(请参阅 preg_split)并以数字方式比较每个元素(如果两者都是数字)或使用字符串比较(当两者都是字母数字时)适用于您的示例:
它可能会失败的是像 5.2-alpha-foo 与 5.2.49.4-beta-bar 这样的版本,您必须比较纯数字带有字母数字子字符串的子字符串:
每当您将纯数字子字符串与字母数字子字符串进行比较时,您都可以通过将字母数字字段视为 0 来解决此问题。
Splitting by symbol (see preg_split) and comparing each element numerically (if both are numeric) or using string comparison (when both are alphanumeric) works for your examples:
Where it potentially falls down is a version like 5.2-alpha-foo vs 5.2.49.4-beta-bar where you must compare a purely numeric sub-string with an alphanumeric sub-string:
You could solve this by treating the alphanumeric field as 0 any time you have a purely numeric sub-string compared against an alphanumeric sub string.
作为参考,rpm 比较版本字符串如下所示:
这有缺陷: 1.2.3rc1 > 1.2.3 和1.2.3alpha > 1.2.3 可能不正确
For reference rpm compare version strings something like this:
This has flaws: 1.2.3rc1 > 1.2.3 and 1.2.3alpha > 1.2.3 which may not be right