混合内容的PHP增量

混合内容的PHP增量,php,increment,Php,Increment,请问,你能帮我用一个PHP函数来增加混合内容(0-9,a-Z,,-)吗 private function increment_mix($id) { $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-" // I can not figure it out. } echo $this->increment_mix("5"); // 6 echo $this->incre

请问,你能帮我用一个PHP函数来增加混合内容(0-9,a-Z,,-)吗

private function increment_mix($id) {
    $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"

    // I can not figure it out.
}

echo $this->increment_mix("5"); // 6
echo $this->increment_mix("Y"); // Z
echo $this->increment_mix("-"); // 00
echo $this->increment_mix("00"); // 01
echo $this->increment_mix("0z"); // 0A
echo $this->increment_mix("0-"); // 10
echo $this->increment_mix("mo"); // mp
echo $this->increment_mix("V--"); // W00

下面是一个将完成此工作的函数:

<?php
function increment_mix($id) {
    $chars = str_split("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-");
    $parts = str_split(strrev($id));

    $firstChar = "first";

    if(isset($parts[0])){
        $firstChar = $parts[0];
    }

    $parts[0] = "increace|".$firstChar;

    for($i = 0; $i < count($parts); $i++){
        if(strpos($parts[$i], "increace|") !== false){
            $charPart = str_replace("increace|", "", $parts[$i]);

            if($charPart == end($chars)){
                $parts[$i] = $chars[0];

                $nextChar = "first";

                if(isset($parts[$i+1])){
                    $nextChar = $parts[$i+1];
                }

                $parts[$i+1] = "increace|".$nextChar;
            }else{
                if($charPart == "first"){
                    $parts[$i] = $chars[0];
                }else{
                    $parts[$i] = $chars[array_search($charPart, $chars) + 1];
                }
            }
        }
    }

    return strrev(implode("", $parts));
}
?>


编辑:如果您想以其他顺序使用其他字符,只需编辑第3行
str_split()
中的字符串,Marnix的答案并不错误,但我认为这里的逻辑更容易理解

<?php

function increment_mix($id)
{
    // Set the initial position to the rightmost character
    $position = strlen($id) - 1;

    // While we haven't finished incrementing
    while (true) {
        // If we are trying to increment the position left of the first
        // character, then we should just append a 0 to the front and return
        if ($position < 0) {
            $id = '0' . $id;
            break;
        }

        // Try incrementing the single character at the current position
        $result = increment_single($id[$position]);

        if ($result === false) {
            // The current position resulted in a carry. Set the current
            // position to 0 and move our position left one character
            $id[$position] = 0;
            $position--;
        } else {
            // The current position did not result in a carry. Replace the
            // current position with the result from the single increment
            // and return.
            $id[$position] = $result;
            break;
        }
    }

    return $id;
}

/**
 * Increments a single character. Returns false if the operation resulted in
 * a carry.
 */
function increment_single($character)
{
    $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";

    // Find the position of the character
    $position = strpos($chars, $character);
    // Increment it by 1
    $position++;

    if ($position >= strlen($chars)) {
        // The new position is past the end of the $chars string; carry
        return false;
    } else {
        // Return the char at the new position
        return $chars[$position];
    }
}

echo increment_mix("5"); // 6
echo "\n";
echo increment_mix("Y"); // Z
echo "\n";
echo increment_mix("-"); // 00
echo "\n";
echo increment_mix("00"); // 01
echo "\n";
echo increment_mix("0z"); // 0A
echo "\n";
echo increment_mix("0-"); // 10
echo "\n";
echo increment_mix("mo"); // mp
echo "\n";
echo increment_mix("V--"); // W00
echo "\n";

分享你的研究成果对每个人都有帮助。告诉我们您尝试了什么,以及为什么它不能满足您的需求。这表明你花了时间来帮助自己,它使我们避免重复显而易见的答案,最重要的是,它帮助你得到一个更具体和相关的答案!另请参见。您的预期输出看起来有点不一致。首先,您应该在increment_mix函数($id)、srtlen($id)中计算字符串。如果strlen($id)是1,那么$id+1的strpos。。这会让你通过第一部分,自己尝试第二部分。。。提示:断点在-charachter nex char is 0上。第一部分很简单,但第二部分不是…如果字符串中有两个字符,请查找第二个字符的值,如果不是-,则增加其值(再次使用strpos),并注意php中区分大小写/不区分大小写的函数,如果第二个字符是-增加第一个和第二个字符非常感谢你,我不得不承认这对我来说真的太不合逻辑了。也谢谢你:)