PHP Version Processing class

Source: Internet
Author: User
PHP Version Processing class

For example, recording the app version, or some version of the data, if you use 1.0.0 this version format to record the storage, when you need to filter the query will be more troublesome.
Instead of converting the version string to a digital save, you can easily compare and filter versions.

For example, to query the version between 3.0.1 and 10.0.1, because 3.0.1 is larger than 10.0.1 (string comparison), it needs to be processed before it can be queried.
And the 3.0.1 and 10.0.1 first converted to the number 30001 and 100001来 comparison query, it is very convenient.

Version.class.php


    /** * Version processing class, provide version and number to convert each other, convenient storage after comparison filter * date:2015-06-30 * author:fdipzone * ver:1.0 * * Func: * Public Version_to_integer Convert the version to a number * Public integer_to_version convert the number to version * Public Check that the version format is correct * p Ublic Compare Compare the values of two versions * / classVersion{//class start/** * Convert version to digital * @param String $version version * @return Int */ Public functionversion_to_integer($version){if($this->check ($version)){List($major,$minor,$sub) = Explode ('.',$version);$integer _version=$major*10000+$minor* -+$sub;returnIntval ($integer _version); }Else{ThrowNewErrorexception (' version Validate Error '); }    }/** * Converts a number to version * @param Int $version _code version of the number representation * @return String * * Public functioninteger_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/ -);$version[2] =$version _code% -;returnImplode ('.',$version); }Else{ThrowNewErrorexception (' version code Validate Error '); }    }/** * Check that the version format is correct * @param String $version version * @return Boolean */ Public functioncheck($version){$ret= Preg_match ('/^[0-9]{1,3}\. [0-9] {1,2}\. [0-9] {1,2}$/',$version);return$ret?true:false; }/** * Compare two versions of values * @param string $version 1 version 1 * @param string $version 2 version 2 * @return int-1:1<2, 0: Equal, 1:1>2 */ Public functioncompare($version 1, $version 2){if($this->check ($version 1) &&$this->check ($version 2)){$version 1_code=$this->version_to_integer ($version 1);$version 2_code=$this->version_to_integer ($version 2);if($version 1_code>$version 2_code){return1; }ElseIf($version 1_code<$version 2_code){return-1; }Else{return0; }        }Else{ThrowNewErrorexception (' Version1 or version2 Validate Error '); }    }}//class end?>

demo.php


   require' Version.class.php ';$version=' 2.7.1 ';$obj=NewVersion ();//version to digital$version _code=$obj->version_to_integer ($version);Echo$version _code.'
';//20701//Digital to version$version=$obj->integer_to_version ($version _code);Echo$version.'
';//2.7.1//Check version$version=' 1.1.a '; Var_dump ($obj->check ($version));//False//Comparison of two versions$version 1=' 2.9.9 ';$version 2=' 10.0.1 ';$result=$obj->compare ($version 1,$version 2);Echo$result;//-1?>

SOURCE Download Address: Click to view

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the PHP version processing class, including the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.