Home > Article > Backend Development > php version processing class
php version processing class
For example, if you record the app version or some version data, if you use the version format of 1.0.0 to record it into the database, it will be more troublesome when you need to filter the query.
Converting version strings to numbers can facilitate comparison and filtering between 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 querying.
It is very convenient to convert 3.0.1 and 10.0.1 into numbers 30001 and 100001 for comparison and query.
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>
Source code download address: Click to view
Copyright statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces the PHP version processing class, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.