Rumah > Artikel > pembangunan bahagian belakang > 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
<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:12 */</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>$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教程有兴趣的朋友有所帮助。