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
<code><span><span><?php</span><span>/** * 版本处理类,提供版本与数字互相转换,方便入库后进行比较筛选 * Date: 2015-06-30 * Author: fdipzone * ver: 1.0 * * Func: * public version_to_integer 将版本转为数字 * public integer_to_version 将数字转为版本 * public check 检查版本格式是否正确 * public compare 比较两个版本的值 */</span><span><span>class</span><span>Version</span>{</span><span>// class start</span><span>/** * 将版本转为数字 *<span> @param</span> String $version 版本 *<span> @return</span> Int */</span><span>public</span><span><span>function</span><span>version_to_integer</span><span>(<span>$version</span>)</span>{</span><span>if</span>(<span>$this</span>->check(<span>$version</span>)){ <span>list</span>(<span>$major</span>, <span>$minor</span>, <span>$sub</span>) = explode(<span>'.'</span>, <span>$version</span>); <span>$integer_version</span> = <span>$major</span>*<span>10000</span> + <span>$minor</span>*<span>100</span> + <span>$sub</span>; <span>return</span> intval(<span>$integer_version</span>); }<span>else</span>{ <span>throw</span><span>new</span> ErrorException(<span>'version Validate Error'</span>); } } <span>/** * 将数字转为版本 *<span> @param</span> Int $version_code 版本的数字表示 *<span> @return</span> String */</span><span>public</span><span><span>function</span><span>integer_to_version</span><span>(<span>$version_code</span>)</span>{</span><span>if</span>(is_numeric(<span>$version_code</span>) && <span>$version_code</span>>=<span>10000</span>){ <span>$version</span> = <span>array</span>(); <span>$version</span>[<span>0</span>] = (int)(<span>$version_code</span>/<span>10000</span>); <span>$version</span>[<span>1</span>] = (int)(<span>$version_code</span>%<span>10000</span>/<span>100</span>); <span>$version</span>[<span>2</span>] = <span>$version_code</span>%<span>100</span>; <span>return</span> implode(<span>'.'</span>, <span>$version</span>); }<span>else</span>{ <span>throw</span><span>new</span> ErrorException(<span>'version code Validate Error'</span>); } } <span>/** * 检查版本格式是否正确 *<span> @param</span> String $version 版本 *<span> @return</span> Boolean */</span><span>public</span><span><span>function</span><span>check</span><span>(<span>$version</span>)</span>{</span><span>$ret</span> = preg_match(<span>'/^[0-9]{1,3}\.[0-9]{1,2}\.[0-9]{1,2}$/'</span>, <span>$version</span>); <span>return</span><span>$ret</span>? <span>true</span> : <span>false</span>; } <span>/** * 比较两个版本的值 *<span> @param</span> String $version1 版本1 *<span> @param</span> String $version2 版本2 *<span> @return</span> Int -1:1<2, 0:相等, 1:1>2 */</span><span>public</span><span><span>function</span><span>compare</span><span>(<span>$version1</span>, <span>$version2</span>)</span>{</span><span>if</span>(<span>$this</span>->check(<span>$version1</span>) && <span>$this</span>->check(<span>$version2</span>)){ <span>$version1_code</span> = <span>$this</span>->version_to_integer(<span>$version1</span>); <span>$version2_code</span> = <span>$this</span>->version_to_integer(<span>$version2</span>); <span>if</span>(<span>$version1_code</span>><span>$version2_code</span>){ <span>return</span><span>1</span>; }<span>elseif</span>(<span>$version1_code</span><<span>$version2_code</span>){ <span>return</span> -<span>1</span>; }<span>else</span>{ <span>return</span><span>0</span>; } }<span>else</span>{ <span>throw</span><span>new</span> ErrorException(<span>'version1 or version2 Validate Error'</span>); } } } <span>// class end</span><span>?></span></span></code>
demo.php
<code><span><?php</span><span>require</span><span>'Version.class.php'</span>; <span>$version</span> = <span>'2.7.1'</span>; <span>$obj</span> = <span>new</span> Version(); <span>// 版本转数字</span><span>$version_code</span> = <span>$obj</span>->version_to_integer(<span>$version</span>); <span>echo</span><span>$version_code</span>.<span>'<br>'</span>; <span>// 20701</span><span>// 数字转版本</span><span>$version</span> = <span>$obj</span>->integer_to_version(<span>$version_code</span>); <span>echo</span><span>$version</span>.<span>'<br>'</span>; <span>// 2.7.1</span><span>// 检查版本</span><span>$version</span> = <span>'1.1.a'</span>; var_dump(<span>$obj</span>->check(<span>$version</span>)); <span>// false</span><span>// 比较两个版本</span><span>$version1</span> = <span>'2.9.9'</span>; <span>$version2</span> = <span>'10.0.1'</span>; <span>$result</span> = <span>$obj</span>->compare(<span>$version1</span>, <span>$version2</span>); <span>echo</span><span>$result</span>; <span>// -1</span><span>?></span></span></code>
源碼下載地址:點選查看
源碼下載地址:點擊查看博主
以上就介紹了php 版本處理類,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。