我怎样才能制作一个“数字”? PHP 中的字符串?

发布于 2024-10-16 00:34:50 字数 297 浏览 5 评论 0原文

不使用 [0..9] 符号,而使用 [0..9A..Z] 符号

不使用以 10 为基数的系统,而使用以 64 为基数的系统

我想创建一个如下示例所示的函数:

next( 'ABC') return 'ACA' - 这是下一个有 3 个单位的字符串

这就像我们有从 0 到 9 的数字,函数返回下一个数字

数字

next2(135) return 136 - 这是下一个有 3 位数字的 使用基于 10 的数字系统,我想使用数字字母,这意味着基于 36 的系统,并获取下一个所谓的数字

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

愚人国度 2024-10-23 00:34:50

这是一个在 3 进制字母数字系统中生成下一个值的函数:

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

请注意,结果是“CA”,因为“ACA”与在 10 进制中写入“06”相同...我们通常不会这样写前导零,这样你就不会写前导“A”。

因此,我添加了一个填充参数,可让您指定要填充的位数。使用 $pad=3,您可以从“ABC”得到“ACA”。

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

黎夕旧梦 2024-10-23 00:34:50

像这样的东西

<?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'));

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'));
薄暮涼年 2024-10-23 00:34:50

我现在能想到的一种方法是将字符转换为base26,然后在数字上加1,然后将其转换回来,我希望你能明白。
Next2 应该做同样的事情,但使用默认的 base10,所以我这只是数字本身的 +1。
期待看到关于此的其他实现。

编辑:没注意到你最后有A。我真傻我才这么做。那将是 base3 而不是 26。

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.

寄离 2024-10-23 00:34:50

根据您对 Xavier Barbosa 的回答的评论,如果您想使用从 a 到 z 的所有字母,您可以这样做:

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

这将打印:aka

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

一生独一 2024-10-23 00:34:50

这些手动基数转换函数不会受到内置函数的不准确性的影响。

<?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;
    }
?>

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;
    }
?>
叹倦 2024-10-23 00:34:50

要获取 base36 中的下一个数字,请使用:

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

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文