PHP增量混合内容

时间:2016-10-11 18:56:00

标签: 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->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

2 个答案:

答案 0 :(得分:1)

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";

答案 1 :(得分:0)

这是一个能完成这项工作的功能:

<?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()中的字符串