| // | Aidan Lister | // +----------------------------------------------------------------------+ // // $Id: version_compare.php,v 1.1 2005/07/11 16:34:36 ggiunta Exp $ /** * Replace version_compare() * * @category PHP * @package PHP_Compat * @link http://php.net/function.version_compare * @author Philippe Jausions * @author Aidan Lister * @version $Revision: 1.1 $ * @since PHP 4.1.0 * @require PHP 4.0.0 (user_error) */ if (!function_exists('version_compare')) { function version_compare($version1, $version2, $operator = '<') { // Check input if (!is_scalar($version1)) { user_error('version_compare() expects parameter 1 to be string, ' . gettype($version1) . ' given', E_USER_WARNING); return; } if (!is_scalar($version2)) { user_error('version_compare() expects parameter 2 to be string, ' . gettype($version2) . ' given', E_USER_WARNING); return; } if (!is_scalar($operator)) { user_error('version_compare() expects parameter 3 to be string, ' . gettype($operator) . ' given', E_USER_WARNING); return; } // Standardise versions $v1 = explode('.', str_replace('..', '.', preg_replace('/([^0-9\.]+)/', '.$1.', str_replace(array('-', '_', '+'), '.', trim($version1))))); $v2 = explode('.', str_replace('..', '.', preg_replace('/([^0-9\.]+)/', '.$1.', str_replace(array('-', '_', '+'), '.', trim($version2))))); // Replace empty entries at the start of the array while (empty($v1[0]) && array_shift($v1)) {} while (empty($v2[0]) && array_shift($v2)) {} // Release state order // '#' stands for any number $versions = array( 'dev' => 0, 'alpha' => 1, 'a' => 1, 'beta' => 2, 'b' => 2, 'RC' => 3, '#' => 4, 'p' => 5, 'pl' => 5); // Loop through each segment in the version string $compare = 0; for ($i = 0, $x = min(count($v1), count($v2)); $i < $x; $i++) { if ($v1[$i] == $v2[$i]) { continue; } $i1 = $v1[$i]; $i2 = $v2[$i]; if (is_numeric($i1) && is_numeric($i2)) { $compare = ($i1 < $i2) ? -1 : 1; break; } // We use the position of '#' in the versions list // for numbers... (so take care of # in original string) if ($i1 == '#') { $i1 = ''; } elseif (is_numeric($i1)) { $i1 = '#'; } if ($i2 == '#') { $i2 = ''; } elseif (is_numeric($i2)) { $i2 = '#'; } if (isset($versions[$i1]) && isset($versions[$i2])) { $compare = ($versions[$i1] < $versions[$i2]) ? -1 : 1; } elseif (isset($versions[$i1])) { $compare = 1; } elseif (isset($versions[$i2])) { $compare = -1; } else { $compare = 0; } break; } // If previous loop didn't find anything, compare the "extra" segments if ($compare == 0) { if (count($v2) > count($v1)) { if (isset($versions[$v2[$i]])) { $compare = ($versions[$v2[$i]] < 4) ? 1 : -1; } else { $compare = -1; } } elseif (count($v2) < count($v1)) { if (isset($versions[$v1[$i]])) { $compare = ($versions[$v1[$i]] < 4) ? -1 : 1; } else { $compare = 1; } } } // Compare the versions if (func_num_args() > 2) { switch ($operator) { case '>': case 'gt': return (bool) ($compare > 0); break; case '>=': case 'ge': return (bool) ($compare >= 0); break; case '<=': case 'le': return (bool) ($compare <= 0); break; case '==': case '=': case 'eq': return (bool) ($compare == 0); break; case '<>': case '!=': case 'ne': return (bool) ($compare != 0); break; case '': case '<': case 'lt': return (bool) ($compare < 0); break; default: return; } } return $compare; } } ?>='n91' href='#n91'>91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179