note 88440 deleted from function.version-compare by danbrown
| From: | danbrown@php.net | Date: | Sat, 24 Jan 2009 00:48:06 +0000 |
| Subject: | note 88440 deleted from function.version-compare by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149724@lists.php.net to get a copy of this message | ||
Note Submitter: martin_georgiev12345 at abv dot bg
----
Here's another one :)
Some ideas from around the documentation are borrowed and put together into this function
<?
function versionCompare($version1, $operator, $version2) {
//get rid of stuff after the 'clean' version: remove '-abc' from
'5.2.6-abc'
if ( $v1 = substr($version1, 0, strpos($version1, '-') ) ) ; else $v1 = $version1;
if ( $v2 = substr($version2, 0, strpos($version2, '-') ) ) ; else $v2 = $version2;
// make sure versions like 5.2.0/5.2 and 5.2./5.2.0 and some other variations are treated as equal
if ( strlen($v1) != strlen($v2) ) {
$m1 = explode('.', $v1);
$m2 = explode('.', $v2);
if ( ( empty($m1[2]) || $m1[2]=='0' ) && ( empty($m2[2]) ||
$m2[2]=='0' ) ) {
unset($m1[2]);
unset($m2[2]);
if ( ( empty($m1[1]) || $m1[1]=='0' ) && ( empty($m2[1]) ||
$m2[1]=='0' ) ) {
unset($m1[1]);
unset($m2[1]);
if ( ( empty($m1[0]) || $m1[0]=='0' ) && ( empty($m2[0]) ||
$m2[0]=='0' ) ) {
unset($m1[0]);
unset($m2[0]);
}
}
}
$v1 = implode('.', $m1);
$v2 = implode('.', $m2);
//echo '<br />v1='.$v1.' v2='.$v2.'<br />';
}
return version_compare( $v1, $v2, $operator );
}
?>