float に変換してからインクリメントしてから文字列に戻すにはどうすればよいですか。
if($set==$Data_Id)
{
$rel='1.1.1.2';
}
増分後は 1.1.1.3 のようになります。
助けてください。
とてもクレイジー、うまくいくかもしれません
$rel='1.1.1.2';
echo substr($rel, 0, -1). (substr($rel,-1)+1); //1.1.1.3
大きな問題は、文字列が9で終わる場合、どうしたいですか??
ここでは、少し異なるアプローチを示します。
<?php
function increment_revision($version) {
return preg_replace_callback('~[0-9]+$~', function($match) {
return ++$match[0];
}, $version);
}
echo increment_revision('1.2.3.4'); //1.2.3.5
アンソニー。
これがまさに解決する必要がある場合は、 intval ()、strval()、str_replace()、substr()、およびstrlen()を使用して解決できます。
$rel = '1.1.1.2'; // '1.1.1.2'
// replace dots with empty strings
$rel = str_replace('.', '', $rel); // '1112'
// get the integer value
$num = intval($rel); // 1112
// add 1
$num += 1; // 1113
// convert it back to a string
$str = strval($num); // '1113'
// initialize the return value
$ret = '';
// for each letter in $str
for ($i=0; $i<strlen($str); $i++) {
echo "Current ret: $ret<br>";
$ret .= $str[$i] . '.'; // append the current letter, then append a dot
}
$ret = substr($ret, 0, -1); // remove the last dot
echo "Incremented value: " . $ret;
ただし、この方法では 1.1.1.9 が 1.1.2.0 に変更されます。それがあなたの望みなら、これでいいのです。
「1.1.1.2」は有効な数値ではありません。したがって、次のようにする必要があります。
$rel = '1.1.1.2';
$relPlusOne = increment($rel);
function increment($number) {
$parts = explode('.', $number);
$parts[count($parts) - 1]++;
return implode('.', $parts);
}