php 版本處理類,php處理類

來源:互聯網
上載者:User

php 版本處理類,php處理類
php 版本處理類

例如記錄app版本,或某些版本資料,如果使用1.0.0這種版本格式記錄入庫,在需要篩選查詢時會比較麻煩。
而把版本字串轉為數字儲存,可以方便版本間的比較和篩選。

例如:要查詢3.0.1 與 10.0.1之間的版本,因為3.0.1比10.0.1大(字串比較),因此需要處理才可以查詢。
而把 3.0.1 和 10.0.1 先轉為數字 30001 和 100001來比較查詢,則很方便。

Version.class.php

<?php/** * 版本處理類,提供版本與數字互相轉換,方便入庫後進行比較篩選 *  Date:       2015-06-30 *  Author:     fdipzone *  ver:        1.0 * *  Func: *  public  version_to_integer  將版本轉為數字 *  public  integer_to_version  將數字轉為版本 *  public  check               檢查版本格式是否正確 *  public  compare             比較兩個版本的值 */class Version{ // class start    /**     * 將版本轉為數字     * @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');        }    }    /**     * 將數字轉為版本     * @param  Int     $version_code 版本的數字表示     * @return String     */    public function integer_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{            throw new ErrorException('version code Validate Error');        }    }    /**     * 檢查版本格式是否正確     * @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;    }    /**     * 比較兩個版本的值     * @param  String  $version1  版本1     * @param  String  $version2  版本2     * @return Int                -1:1<2, 0:相等, 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($version1_code>$version2_code){                return 1;            }elseif($version1_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();// 版本轉數字$version_code = $obj->version_to_integer($version);echo $version_code.'<br>';  // 20701// 數字轉版本$version = $obj->integer_to_version($version_code);echo $version.'<br>'; // 2.7.1// 檢查版本$version = '1.1.a';var_dump($obj->check($version)); // false// 比較兩個版本$version1 = '2.9.9';$version2 = '10.0.1';$result = $obj->compare($version1, $version2);echo $result; // -1?>

源碼:點擊查看

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.