我知道這可能是晚來回答這個問題,但這裏是我使用的功能:
if(!function_exists('CompareVersion')){
function CompareVersion($v1='', $v2='', $s='>'){
# We delete all characters except numbers 0-9
$regex = '/[^0-9]/';
$v1 = preg_replace($regex, '', $v1);
$v2 = preg_replace($regex, '', $v2);
# Wewill get the length of both string
$lgt1 = strlen($v1);
$lgt2 = strlen($v2);
# We will make sure that the length is the same by adding zeros at the end
# Example: 1031 and 30215 - 1031 is smaller then 1031 become 10310
if($lgt2 > $lgt1){
$v1 = str_pad($v1, $lgt2, 0, STR_PAD_RIGHT);
} elseif($lgt1 > $lgt2){
$v2 = str_pad($v2, $lgt1, 0, STR_PAD_RIGHT);
}
# We remove the leading zeros
$v1 = ltrim($v1, 0);
$v2 = ltrim($v2, 0);
# We return the result
switch($s){
case '>': return $v1 > $v2;
case '>=': return $v1 >= $v2;
case '<': return $v1 < $v2;
case '<=': return $v1 <= $v2;
case '=':
case '==': return $v1 == $v2;
case '===': return $v1 === $v2;
case '<>':
case '!=': return $v1 != $v2;
case '!==': return $v1 !== $v2;
}
}
}
我從來不知道這一點,這是一個方便的功能。不幸的是Nanne是他們的答案,在這裏,如果它不是一個0.0.0格式返回誤報正確。雖然我相信我的項目實際上是0.0.0格式,但這可能會奏效。 – chris