简体   繁体   中英

How can I make a “number” with string in PHP?

Instead of using the [0..9] symbols use the [0..9A..Z] symbols

Instead of using the base-10 system use the base-64 system

I want to make a function like in this example:

next('ABC') return 'ACA' - this is the next string with 3 units

It is like we have numbers from 0 to 9 and the function return the next number

next2(135) return 136 - this is the next number with 3 digits

We use a base-10 system for numbers an I want to use numbersletters that means a base-36 system, and get the next so called number

Here's a function that produces the next value in your base-3 alphabetic number system:

function nextval($input, $pad = 1) {

        $map = array(0 => 'A', 1 => 'B', 2 => 'C');

        //convert letters to numbers
        $num = '';
        for ($i = 0; $i < strlen($input); $i++) {
                $num .= array_search($input{$i}, $map);
        }

        //convert the number to base 10, then add 1 to it
        $base10 = base_convert($num, 3, 10);
        $base10++;

        //convert back to base 3
        $base3 = base_convert($base10, 10, 3);

        //swap the digits back to letters
        $num = '';
        for ($i = 0; $i < strlen($base3); $i++) {
                $num .= $map[$base3{$i}];
        }

        //pad with leading A's
        while (strlen($num) < $pad) {
                $num = 'A' . $num;
        }

        return $num;

}

echo nextval('ABC', 3); //ACA

Note that the result is "CA" as "ACA" is the same as writing "06" in base-10... we don't usually write leading zeros so you wouldn't write leading "A"s.

I therefore added a pad parameter that lets you specify what number of digits you want to pad to. With $pad=3 , you get "ACA" as next from "ABC".

Something like this

<?php

function toNext($input) {
    $conv = strtr(strtolower($input), array(
        'a' => '0',
        'b' => '1',
        'c' => '2' ));
    $conv = base_convert($conv, 3, 10);
    $conv++;
    $output = base_convert($conv, 10, 3);
    $output = sprintf("%03d", $output); 
    $output = strtr((string) $output, array(
        '0' => 'a',
        '1' => 'b',
        '2' => 'c' ));
    return strtoupper($output);
}


var_dump(toNext('ABC'));
var_dump(toNext('ABA'));

One way I could think of right now is converting the characters to base26 and then adding 1 to the number and converting it back, I hope you get the idea. Next2 should do a same but with base10 which is default so I that would just be a +1 to the number itself. Looking forward to see other implementations on this one.

Edit: Didn't notice you had A at the end. silly me of me to do that. that would be base3 then instead of 26.

These manual base conversion functions don't suffer from the inaccuracies of the built-in ones.

<?php
function next($str)
    {
    $baseDec = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    $baseAln = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
    return base_conv((string) ((int) base_conv($str, $baseAln, $baseDec) + 1), $baseDec, $baseAln);
    }



function base_conv_str($valStr, $baseToStr, $baseFromStr)
    {
    $baseTo = str_split($baseToStr);
    $baseFrom = str_split($baseFromStr);
    return base_arr_to_str(base_conv_arr(base_str_to_arr((string) $valStr, $baseFrom), count($baseTo), count($baseFrom)), $baseTo);
    }

function base_conv($valStr, &$baseTo, &$baseFrom)
    {
    return base_arr_to_str(base_conv_arr(base_str_to_arr((string) $valStr, $baseFrom), count($baseTo), count($baseFrom)), $baseTo);
    }

function base_conv_arr($val, $baseToDigits, $baseFromDigits)
    {
    $valDigits = count($val);
    $result = array();
    do
        {
        $divide = 0;
        $newlen = 0;
        for ($i = 0; $i < $valDigits; ++$i)
            {
            $divide = $divide * $baseFromDigits + $val[$i];
            if ($divide >= $baseToDigits)
                {
                $val[$newlen ++] = (int) ($divide / $baseToDigits);
                $divide = $divide % $baseToDigits;
                }
            else if ($newlen > 0)
                {
                $val[$newlen ++] = 0;
                }
            }
        $valDigits = $newlen;
        array_unshift($result, $divide);
        }
        while ($newlen != 0);
    return $result;
    }

function base_arr_to_str($arr, &$base)
    {
    $str = '';
    foreach ($arr as $digit)
        {
        $str .= $base[$digit];
        }
    return $str;
    }

function base_str_to_arr($str, &$base)
    {
    $arr = array();
    while ($str === '0' || !empty($str))
        {
        foreach ($base as $index => $digit)
            {
            if (mb_substr($str, 0, $digitLen = mb_strlen($digit)) === $digit)
                {
                $arr[] = $index;
                $str = mb_substr($str, $digitLen);
                continue 2;
                }
            }
        throw new Exception();
        }
    return $arr;
    }
?>

According to your comment to Xavier Barbosa's answer, if you want to use all letters from a to z you can do:

$str = 'ajz';
echo ++$str,"\n";

this will print : aka

To get the next number in base36 use:

function next_base36($n) {
    $n = base_convert($n, 36, 10);
    return base_convert($n + 1, 10, 36);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM