這篇“php字符串增加1如何實現(xiàn)”文章的知識點大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“php字符串增加1如何實現(xiàn)”文章吧。
南平網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),南平網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為南平近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的南平做網(wǎng)站的公司定做!
php字符串增加1的實現(xiàn)方法:1、新建php示例文件;2、創(chuàng)建“function inc($s) {...}”方法;3、通過“while ($n-- > 0) {$s = inc($s);}”計算除去最后一個字符的字符串,然后進位即可。
php 數(shù)字字符串 +1 操作 累加 加一 (不溢出)
* inc.php
<?php
function inc($s) {
if (empty($s)) {return "1";}
if ( ! isset($s[1]) ) {
$code = ord($s[0]) + 1;
if ($code <= ord("9")) {
return chr($code);
}
return "10";
}
$n = strlen($s);
$code = ord($s[$n-1]) +1;
if ($code <= ord("9")) {
return substr($s, 0, $n-1).chr($code);
}
return inc(substr($s, 0, $n-1))."0";
}
$s = "2147483647";
$n = 10000;
while ($n-- > 0) {
// printf("%s\n", $s);
$s = inc($s);
}
printf("%s\n", $s); // "2147493647"
遞歸,如果需要進位, 先計算除去最后一個字符的字符串, 最后一個字符置為'0'; 單個數(shù)字字符0-9顯然好處理。
* test:
<?php
$m = 2147483647;
$n = 10000;
while ($n-- > 0) {
// printf("%d\n", $m);
$m++;
}
printf("%d\n", $m);
比較結(jié)果一致
* add2.php // 遍歷1-4位大寫字母
<?php
function add($s) {
if (empty($s)) {return "A";}
if ( ! isset($s[1]) ) {
$code = ord($s[0]) + 1;
if ($code <= ord("Z")) {
return chr($code);
}
return "AA";
}
$n = strlen($s);
$code = ord($s[$n-1]) +1;
if ($code <= ord("Z")) {
return substr($s, 0, $n-1).chr($code);
}
return add(substr($s, 0, $n-1))."A";
}
$s = "";
do {
$s = add($s);
printf("%s\n", $s);
} while ($s != "ZZZZ");
以上就是關(guān)于“php字符串增加1如何實現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對大家有幫助,若想了解更多相關(guān)的知識內(nèi)容,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:php字符串增加1如何實現(xiàn)
分享路徑:http://jinyejixie.com/article2/pddgic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、面包屑導(dǎo)航、微信公眾號、商城網(wǎng)站、網(wǎng)站制作、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)