在我的應用程序中,用戶可以在輸入字段中添加版本號,假設他最初添加了一個類似於1.0.1的版本號,並且他必須在下次輸入更高的版本號,我如何驗證它在PHP中。版本號的輸入字符串的正則表達式
不同類型的版本號
1.0
1.0.1
1.01
它可以任意組合
誰能幫助我在這裏?
在我的應用程序中,用戶可以在輸入字段中添加版本號,假設他最初添加了一個類似於1.0.1的版本號,並且他必須在下次輸入更高的版本號,我如何驗證它在PHP中。版本號的輸入字符串的正則表達式
不同類型的版本號
1.0
1.0.1
1.01
它可以任意組合
誰能幫助我在這裏?
你可以試試這個。
$version="1.0.1.1";
$versionnew="1.0.1.0";
//explode the version to be able to compare each and every part of the version
$versionArray = explode(".",$version);
$versionnewArray = explode(".",$versionnew);
//the maximum depth of comparison
$limit = min(count($versionArray),count($versionnewArray));
for($i=0;$i<$limit;$i++){
if($versionArray[$i] == $versionnewArray[$i]){
continue;
}else if($versionArray[$i] > $versionnewArray[$i]){
echo $versionnew . " should be newer than " . $version;
}else echo $versionnew . " is newer than " . $version;
}
適用於我,謝謝+1 –
這可以幫助你得到持續
$newVersion= "1.2"; //get a string reference to your version here
$oldVersion = "1.3";
$piecesNew = explode(".", $newVersion); //This will give you an array existing of [1,2]
$piecesOld = explode(".", $oldVersion); //This will give you an array existing of [1,3]
//Do some checking here for each number
是的,這將解決我的問題在一個擴展,但如果版本號是像1.0.1或1.0.0.1或1,等等...我需要一個通用的方式來處理它。 –
爲此,您需要一些if else語句。首先,您可以檢查兩個數組是否具有相同的長度。如果這樣比較它們,如果沒有比較它們,並且如果它們相等,則必須設置新件的最後位置 –
你是說你想驗證新版本號輸入是否比舊版本更高? – rccoros
將版本保留爲主版本和修訂版...並從所有 – user1844933
中選擇最大值是我想驗證新版本號輸入是否高於舊版本號。 –