Php version processing class

Source: Internet
Author: User
: This article mainly introduces the php version processing class. if you are interested in the PHP Tutorial, you can refer to it. 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.
However, it is convenient to convert 3.0.1 and 10.0.1 into numbers 30001 and 100001 to compare queries.

Version. class. php


  Check ($ version) {list ($ major, $ minor, $ sub) = explode ('. ', $ version); $ integer_version = $ major * 10000 + $ minor * 100 + $ sub; return intval ($ integer_version );} else {thrownew ErrorException ('version Validate error ');}} /*** convert the number to version * @ param Int $ version_code indicates * @ return String */publicfunctioninteger_to_version ($ version_code) {if (is_numeric ($ version_code) & $ version_code> = 10000) {$ version = Array (); $ version [0] = (int) ($ version_code/10000); $ version [1] = (int) ($ version_code % 10000/100 ); $ version [2] = $ version_code %100; return implode ('. ', $ version);} else {thrownew ErrorException ('version code Validate error ');}} /*** check whether the version format is correct * @ param String $ version * @ return Boolean */publicfunctioncheck ($ 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 */publicfunctioncompare ($ version1, $ version2) {if ($ this-> check ($ version1) & $ this-> check ($ version2) {$ version1_code = $ this-> version_to_integer ($ version1); $ version2_code = $ this-> version_to_integer ($ version2 ); if ($ version1_code> $ version2_code) {return1;} elseif ($ version1_c Ode <$ version2_code) {return-1 ;}else {return0 ;}} else {thrownew ErrorException ('version1 or version2 Validate error') ;}}// class end?>

Demo. php


  Version_to_integer ($ version); echo $ version_code .'
'; // 20701 // number to version $ version = $ obj-> integer_to_version ($ version_code); echo $ version .'
'; // 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 ($ version1, $ 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.

The above introduces the php version processing class, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.

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.