以数值方式增加 alpha 值

发布于 2024-11-25 01:00:13 字数 576 浏览 5 评论 0 原文

就像标题所暗示的那样,我需要做类似的事情......

$i++;//we all know this.

$value = 'a';

increment($value);// i need this functionality
//output 
string [ b ]

///here are some more samples, to help you understand...

increment('b'); //output// c
increment('z'); //output// A [capital A not fussy but would be good :) ]
increment('9'); //output// a1
increment('a1'); //output// a2
increment('aa1'); //output// aa2

等等...... 更新

好吧,假设我使用数值 $id++; 我最终会得到一个巨大的数字1310743942525; 这可能比“ab34c9”占用更多的空间,我试图保存字符长度以保存在数据库上......

Like the title suggests I need to do something like so...

$i++;//we all know this.

$value = 'a';

increment($value);// i need this functionality
//output 
string [ b ]

///here are some more samples, to help you understand...

increment('b'); //output// c
increment('z'); //output// A [capital A not fussy but would be good :) ]
increment('9'); //output// a1
increment('a1'); //output// a2
increment('aa1'); //output// aa2

and so on...
UPDATE

well lets say I use numeric values
$id++;
I would end up with a massive number eventuall 1310743942525;
which can take alot more space than say `ab34c9" im trying to save length of characters to save on db ...

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

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

发布评论

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

评论(3

允世 2024-12-02 01:00:14

您尝试将其视为 62 基数:
http://www.pgregg.com/projects/php/base_conversion/base_conversion.php

源代码位于

http://www.pgregg.com/projects/php/base_conversion/base_conversion.inc.phps

将其转换为十进制,递增,然后将其转换回基数 62

更新< /strong>

从我阅读代码的方式来看,您可以拥有这样的工作流程:

$value = 'ab8Zb';
$value_base10 = base_base2dec($value, 62);
$value_base10++;
$value = base_dec2base($value_base10, 62); // should be 'ab8Zc'

You try to treat it as a base 62 number:
http://www.pgregg.com/projects/php/base_conversion/base_conversion.php

with source code at

http://www.pgregg.com/projects/php/base_conversion/base_conversion.inc.phps

convert it to decimal, increment, and convert it back to base 62

UPDATE

From how I read the code, you could have a workflow like this:

$value = 'ab8Zb';
$value_base10 = base_base2dec($value, 62);
$value_base10++;
$value = base_dec2base($value_base10, 62); // should be 'ab8Zc'
浪菊怪哟 2024-12-02 01:00:14

如果您只想节省数据库空间,请考虑这一点。

在 MySQL 中,您可以拥有一个类型为 UNSIGNED BIGINT 的字段。该字段的最大大小为18446744073709551615,存储空间只有8个字节。

如果您要将此数字 (1.844 x 10^19) 转换为 base-62,它将表示为 LygHa16AHYF。您需要一个 CHAR(11) (11 字节)或一个 VARCHAR(11) (12 字节)来存储转换后的数字。

如果您使用 VARCHAR 作为字段类型,则较小的数字将占用较少的空间,但对于较大的数字,实际上会占用更多空间。无论如何,8 个字节对于一个巨大的数字来说是相当小的。我会节省精力,只需将 DB 字段设为 UNSIGNED BIGINT 即可。

If all you are trying to do is save database space, consider this.

In MySQL you can have a field with a type UNSIGNED BIGINT. The maximum size of this field is 18446744073709551615 and the storage space is only 8 bytes.

If you were to convert this number (1.844 x 10^19) to base-62, it would be represented as LygHa16AHYF. You would need a CHAR(11) (11 bytes) or a VARCHAR(11) (12 bytes) in order to store the converted number.

If you used VARCHAR for the field type, smaller numbers would take less space, but for the larger numbers it actually takes more. 8 bytes for a huge number is pretty minimal anyway. I would save the effort and just make the DB field a UNSIGNED BIGINT.

习ぎ惯性依靠 2024-12-02 01:00:14

您可以使用每个字母的 ascii 代码。这只是一个向您展示想法的简单示例,当然,如果您想将 'aa1' 增加到 'aa2',则需要进行大量修改,但不可能;P 您只需要编写几个条件即可。

function increment($value) 
{
  if(strlen($value)>1)
    return false; 

  $asciiCode = ord($value); 
  return chr($asciiCode + 1); 
}

http://www.asciitable.com/ - ASCII 代码表 :)

You can use ascii codes of each letter. This is just a simple example that will show you the idea, ofcourse it need a lot of modyfications if you want to increment 'aa1' into 'aa2' but impossible is nothig ;P You will just need to write few conditions.

function increment($value) 
{
  if(strlen($value)>1)
    return false; 

  $asciiCode = ord($value); 
  return chr($asciiCode + 1); 
}

http://www.asciitable.com/ - ASCII codes table :)

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