Skip to content

Instantly share code, notes, and snippets.

@msztorc
Created June 22, 2020 13:18
Show Gist options
  • Save msztorc/b125cec59d5ac9854b52362a111b2585 to your computer and use it in GitHub Desktop.
Save msztorc/b125cec59d5ac9854b52362a111b2585 to your computer and use it in GitHub Desktop.
<?php
function ver_compare($v1, $v2) {
function fversion($v) {
$c = substr_count($v, '.');
switch($c) {
case 0: $v .= '.0.0'; break;
case 1: $v .= '.0'; break;
}
$v = array_map(function($x){return (!is_numeric($x) && $x !== '.') ? (ord($x)) : $x;},str_split($v));
return floatval(preg_replace('/\./', '', implode($v), 1));
}
$_v1 = fversion($v1);
$_v2 = fversion($v2);
if ($_v1 > $_v2)
return $v1;
else
return $v2;
}
$v1 = '0.1.0';
$v2 = '0.1.0a';
echo ver_compare($v1, $v2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment