I'm writing a script in PHP that needs to compare two package version numbers to determine which is newer.

PHP has a handy function called version_compare(), but it gets really confused with some of the more creative version numbers. For example, version_compare() thinks that 0.9.8g-16ubuntu3.1 is newer than 0.9.8o-1ubuntu4.4.

Anyone know how I can get this to work?

I'm happy to run any command line scripts using exec().


take a look to the dpkg --compare-versions option, it will allow you to ask dpkg to do the comparison :)

dpkg --compare-versions 0.9.8g-16ubuntu3.1 lt 0.9.8o-1ubuntu4.4

will return 0, that means 0.9.8g-16ubuntu3.1 is lt (lesser than, < ) to 0.9.8o-1ubuntu4.4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.