Convert a number to a 5-digit string

I want to convert a number to a 5 digit string. String characters: az, AZ, and 0-9. Each combination of this code is incremented by "1"

I do not know how to explain this, so I will give you an example. for instance

1 = aaaaa 26 = aaaaz 27 = aaaaA 52 = aaaaZ 53 = aaaa0 62 = aaaa9 63 = aaaba 89 = aaabz 90 = aaab0 

So, if I have the number 1035, is there a way that PHP can calculate the code for this?

Sorry, my question is a bit vague.

The reason I want to do this is because I don’t want to show the primary key identifier of the database, I want to show this base63 format.

+6
source share
2 answers

I think you could do something like this:

 function custom_base_62($n) { if ($n < 1) trigger_error('custom_base_62(): This silly system cannot represent zero.', E_USER_ERROR); $n -= 1; $symbols = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9)); $r = ''; while ($n) { $r = $symbols[$n % 62] . $r; $n = floor($n / 62); } return str_pad($r, 5, $symbols[0], STR_PAD_LEFT); } 
+1
source

Here is an idea that you can play with, not quite sure that I understand your exact purpose.

 <?php function Digit_to_char($s){ $s1 = str_split($s); while(list($k,$v) = each($s1)){ $s2[] = str_replace(range(0,9), array("a","b","c","d","e","f","g","h","y","z"), $v); } return implode('',$s2); } echo Digit_to_char(12345); // prints bcdef ?> 
+1
source

Source: https://habr.com/ru/post/954799/


All Articles