1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<?
/**
* Create the 'extended version' of the original versions,
* which is basically numerical version schemes padded
* with zeroes to be able to be sorted numerically.
*
* @param array indexed array of original versions (pv)
* @return array of extended versions with original version as key
*/
function extendVersions($arr) {
if(!count($arr))
return array();
// Check if we have any dots in versions
// If we don't, it's fine as is -- just pad the zeroes
if(!(count($arr) && preg_grep('/\./', $arr))) {
foreach($arr as $value) {
$max = max($max, strlen($value));
}
foreach($arr as $key => $value) {
$ext[$key] = str_pad($value, $max, 0, STR_PAD_LEFT);
}
return $ext;
}
// Keep a count of the max number of version breaks (dots)
// array('1.2', '2.12.3') would have a max of 2.
$max = 0;
foreach($arr as $key => $str) {
$max = max($max, substr_count($str, '.'));
$arr_extended[$key] = explode('.', $str);
}
$max++;
// Get the max *numerical* lengths for each split
// 2.12.3 would create: array(1, 2, 1);
foreach($arr_extended as $tmp) {
foreach($tmp as $key => $value) {
$value = preg_replace('/\D/', '', $value);
$arr_max_strlen[$key] = max($arr_max_strlen[$key], strlen($value));
}
}
// Now pad everything to the extended version
foreach($arr as $key => $value) {
// Split the version into each of its dot values
$explode = explode(".", $value);
// Pad and/or create the version for each dot version
for($x = 0; $x < $max; $x++) {
// Yes, you *have* to pad the first number too. Otherwise PHP
// will compare it wrong if there's more than one decimal point.
if($x == 0)
$ext[$key] = str_pad($explode[$x], $arr_max_strlen[$x], 0, STR_PAD_LEFT);
if($x > 0)
$ext[$key] .= ".".str_pad($explode[$x], $arr_max_strlen[$x], 0, STR_PAD_LEFT);
}
}
asort($ext);
return $ext;
}
?>
|