Rev Author Line No. Line
139 root 1 <?php
2 // +----------------------------------------------------------------------+
3 // | PHP Version 4 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997-2004 The PHP Group |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 3.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/3_0.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+
15 // | Authors: Philippe Jausions <Philippe.Jausions@11abacus.com> |
16 // | Aidan Lister <aidan@php.net> |
17 // +----------------------------------------------------------------------+
18 //
19 // $Id: version_compare.php,v 1.13 2005/08/01 12:21:14 aidan Exp $
20  
21  
22 /**
23 * Replace version_compare()
24 *
25 * @category PHP
26 * @package PHP_Compat
27 * @link http://php.net/function.version_compare
28 * @author Philippe Jausions <Philippe.Jausions@11abacus.com>
29 * @author Aidan Lister <aidan@php.net>
30 * @version $Revision: 1.13 $
31 * @since PHP 4.1.0
32 * @require PHP 4.0.5 (user_error)
33 */
34 if (!function_exists('version_compare')) {
35 function version_compare($version1, $version2, $operator = '<')
36 {
37 // Check input
38 if (!is_scalar($version1)) {
39 user_error('version_compare() expects parameter 1 to be string, ' .
40 gettype($version1) . ' given', E_USER_WARNING);
41 return;
42 }
43  
44 if (!is_scalar($version2)) {
45 user_error('version_compare() expects parameter 2 to be string, ' .
46 gettype($version2) . ' given', E_USER_WARNING);
47 return;
48 }
49  
50 if (!is_scalar($operator)) {
51 user_error('version_compare() expects parameter 3 to be string, ' .
52 gettype($operator) . ' given', E_USER_WARNING);
53 return;
54 }
55  
56 // Standardise versions
57 $v1 = explode('.',
58 str_replace('..', '.',
59 preg_replace('/([^0-9\.]+)/', '.$1.',
60 str_replace(array('-', '_', '+'), '.',
61 trim($version1)))));
62  
63 $v2 = explode('.',
64 str_replace('..', '.',
65 preg_replace('/([^0-9\.]+)/', '.$1.',
66 str_replace(array('-', '_', '+'), '.',
67 trim($version2)))));
68  
69 // Replace empty entries at the start of the array
70 while (empty($v1[0]) && array_shift($v1)) {}
71 while (empty($v2[0]) && array_shift($v2)) {}
72  
73 // Release state order
74 // '#' stands for any number
75 $versions = array(
76 'dev' => 0,
77 'alpha' => 1,
78 'a' => 1,
79 'beta' => 2,
80 'b' => 2,
81 'RC' => 3,
82 '#' => 4,
83 'p' => 5,
84 'pl' => 5);
85  
86 // Loop through each segment in the version string
87 $compare = 0;
88 for ($i = 0, $x = min(count($v1), count($v2)); $i < $x; $i++) {
89 if ($v1[$i] == $v2[$i]) {
90 continue;
91 }
92 $i1 = $v1[$i];
93 $i2 = $v2[$i];
94 if (is_numeric($i1) && is_numeric($i2)) {
95 $compare = ($i1 < $i2) ? -1 : 1;
96 break;
97 }
98  
99 // We use the position of '#' in the versions list
100 // for numbers... (so take care of # in original string)
101 if ($i1 == '#') {
102 $i1 = '';
103 } elseif (is_numeric($i1)) {
104 $i1 = '#';
105 }
106  
107 if ($i2 == '#') {
108 $i2 = '';
109 } elseif (is_numeric($i2)) {
110 $i2 = '#';
111 }
112  
113 if (isset($versions[$i1]) && isset($versions[$i2])) {
114 $compare = ($versions[$i1] < $versions[$i2]) ? -1 : 1;
115 } elseif (isset($versions[$i1])) {
116 $compare = 1;
117 } elseif (isset($versions[$i2])) {
118 $compare = -1;
119 } else {
120 $compare = 0;
121 }
122  
123 break;
124 }
125  
126 // If previous loop didn't find anything, compare the "extra" segments
127 if ($compare == 0) {
128 if (count($v2) > count($v1)) {
129 if (isset($versions[$v2[$i]])) {
130 $compare = ($versions[$v2[$i]] < 4) ? 1 : -1;
131 } else {
132 $compare = -1;
133 }
134 } elseif (count($v2) < count($v1)) {
135 if (isset($versions[$v1[$i]])) {
136 $compare = ($versions[$v1[$i]] < 4) ? -1 : 1;
137 } else {
138 $compare = 1;
139 }
140 }
141 }
142  
143 // Compare the versions
144 if (func_num_args() > 2) {
145 switch ($operator) {
146 case '>':
147 case 'gt':
148 return (bool) ($compare > 0);
149 break;
150 case '>=':
151 case 'ge':
152 return (bool) ($compare >= 0);
153 break;
154 case '<=':
155 case 'le':
156 return (bool) ($compare <= 0);
157 break;
158 case '==':
159 case '=':
160 case 'eq':
161 return (bool) ($compare == 0);
162 break;
163 case '<>':
164 case '!=':
165 case 'ne':
166 return (bool) ($compare != 0);
167 break;
168 case '':
169 case '<':
170 case 'lt':
171 return (bool) ($compare < 0);
172 break;
173 default:
174 return;
175 }
176 }
177  
178 return $compare;
179 }
180 }
181  
130 kaklik 182 ?>