Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:将数字转换为固定长度的编码字符串_Php_String_Encoding - Fatal编程技术网

PHP:将数字转换为固定长度的编码字符串

PHP:将数字转换为固定长度的编码字符串,php,string,encoding,Php,String,Encoding,我有很多数字。。有些是一个数字,另一些是多个数字。我需要为每个数字创建一个字符串。字符串的长度必须为8个字符(始终) 我尝试了base64encode,它为每个数字提供了一个漂亮的字符串。。。但对于一位数,我只有3个字符 有人有办法吗 $substitutes = range('a', 'j'); $formatted = sprintf('%010d', $number); $string = preg_replace_callback('/./', function ($m)

我有很多数字。。有些是一个数字,另一些是多个数字。我需要为每个数字创建一个字符串。字符串的长度必须为8个字符(始终)

我尝试了base64encode,它为每个数字提供了一个漂亮的字符串。。。但对于一位数,我只有3个字符

有人有办法吗

$substitutes = range('a', 'j');
$formatted   = sprintf('%010d', $number);
$string      = preg_replace_callback('/./', function ($m) use ($substitutes) {
                   return $substitutes[$m[0]];
               }, $formatted);

echo $string;

这实际上是“蛮力”的版本,我相信有更快捷的方法可以做到这一点。我将让您结合解码算法来解决这些问题。

谢谢。我会试试的。是的,这对我没什么帮助。你只要一开始就把绳子填上。那不漂亮你还想要什么?你有最大位数吗?