Originally Posted by Dridzt
|
some php code that should parse most versions - including the example.
only reason why php, is that it's quicker to play with - as opposed to compiling and UI stuff.
Code:
<?
//pattern - checks for the presence of a "v", with or without space,
//then checks for numbers, dashes and dots - and captures those.
//and finally, recognizes the posibility of whitespace, and looks for any letters following it.
$pat = '/[vV\s]*([0-9\-\.]+)[\s]*([a-zA-Z]?)/';
//test string
$str="v2.08-3a";
//do the match
preg_match($pat,$str,$match);
//replace dashes with dots
$newstr = str_replace("-",".",$match[1]);
//split it by the dots
$sstring = explode(".",$newstr);
//init vars
$ver ="0";
$subver ="0";
//test if there are actually any dots
if(count($sstring)>1)
{
//if so, combine the first two (i.e. 2.08)
$ver=strval($sstring[0]).".".strval($sstring[1]);
//check for moar dots!
if(count($sstring)>2)
{
//loop through the rest, appending any additional parts
for($i=2; $i < count($sstring);$i++)
{
$ver.=strval($sstring[$i]);
}
}
}else{
//otherwise, just set it to the first value.
$ver=$sstring[0];
}
//check if we have a letter;
if(count($match)>2)
{
//if so, drop it low, then get the char value of it - 97 in this case
//char values go up in alphabetical order, but 'A' and 'a' need to be treated the same.
$subver=ord(strtolower($match[2]));
}
//and output the version (2.083) and the subversion (97)
echo "Version: ".$ver."<br>";
echo "SubVersion: ".$subver;
// 2.08.4 will be a newer version, as will 2.08.3b, and so forth.
?>
I'd be willing to take on more "challenges" to put it to the test of course.