Php version processing class, php processing class

Source: Internet
Author: User

Php version processing class, php processing class
Php version processing class

For example, to record the app version or some version data, if you use a version format such as 1.0.0 to record the database, it will be troublesome to filter and query the data.
By converting the version string into a number, you can easily compare and filter versions.

For example, to query versions between 3.0.1 and 10.0.1, because 3.0.1 is larger than 10.0.1 (string comparison), you need to process it before querying.
However3.0.1And10.0.1Convert to number first30001And100001Is convenient.

Version. class. php

<? Php/*** version processing class, which provides conversion between versions and numbers to facilitate comparison and filtering after receiving data * Date: * Author: fdipzone * ver: 1.0 ** Func: * public version_to_integer convert the Version to the number * public integer_to_version convert the number to the Version * public check whether the Version format is correct * public compare the values of two versions */class Version {// class start/ * ** convert the version to a number * @ param String $ version * @ return Int */public function version_to_integer ($ version) {if ($ this-> check ($ version) {list ($ major, $ Minor, $ sub) = explode ('. ', $ version); $ integer_version = $ major * 10000 + $ minor * 100 + $ sub; return intval ($ integer_version );} else {throw new ErrorException ('version Validate error ');}} /*** convert the number to version * @ param Int $ version_code indicates * @ return String */public function integer_to_version ($ version_code) {if (is_numeric ($ version_code) & amp; $ version_code & gt; = 10000) {$ version = array (); $ version [0] = (in T) ($ version_code/10000); $ version [1] = (int) ($ version_code % 10000/100); $ version [2] = $ version_code % 100; return implode ('. ', $ version);} else {throw new ErrorException ('version code Validate error ');}} /*** check whether the version format is correct * @ param String $ version * @ return Boolean */public function check ($ version) {$ ret = preg_match ('/^ [0-9] {1, 3 }\. [0-9] {1, 2 }\. [0-9] {1, 2} $/', $ version); return $ ret? True: false;}/*** compares the values of two versions * @ param String $ version1 version 1 * @ param String $ version2 version 2 * @ return Int-<2, 0: equal, 1:1> 2 */public function compare ($ version1, $ version2) {if ($ this-> check ($ version1) & $ this-> check ($ version2) {$ version1_code = $ this-> version_to_integer ($ version1); $ version2_code = $ this-> version_to_integer ($ version2 ); if ($ versionresponse code> $ version2_code) {return 1;} elseif ($ versi On1_code <$ version2_code) {return-1 ;}else {return 0 ;}} else {throw new ErrorException ('version1 or version2 Validate error ');}}} // class end?>

Demo. php

<? Phprequire 'version. class. php '; $ version = '2. 7.1 '; $ obj = new Version (); // convert version to number $ version_code = $ obj-> version_to_integer ($ Version); echo $ version_code. '<br>'; // 20701 // number to version $ version = $ obj-> integer_to_version ($ version_code); echo $ version. '<br>'; // 2.7.1 // check the version $ version = '1. 1. a'; var_dump ($ obj-> check ($ version); // false // compare two versions $ version1 = '2. 9.9 '; $ version2 = '10. 0.1 '; $ result = $ obj-> compare ($ versi On1, $ version2); echo $ result; //-1?>

Source code: Click to view

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.