PHP dynamic creation of alphabet

懵懂的女人 提交于 2019-12-22 01:22:09

问题


I would like to create some arrys. First of all I would like to tell you what it is about so that you understand why I am doing this:

Cryptography.

I want create an array with the alphabet.

such as

$a1 = array("a"=>"b", "b"=>"c",....,"z"=>a");

Alright, that is just a bit of typing so now I want to do it a bit more often. In this case it is x+1=y or in other words for the decoding x=y-1

So lets say I would like to do that with a position change from 1 to 26 - I would have 26 arrays than.

The encryption and decryption itself is not that problem in php and not what I am asking for as it is simple string replacement. But I was wondering if there is anything like this possible to create in an dynamic way by telling:

createAlphabets(1,12)

and it creates me a multidimensional array with 12 alphabet keys?

This is the second part of my question:

is there mathematically figure of more possibilities to swap characters by calculation?

I mean, x+5-3=y is the same like x+2=y so however I calculate it is covered by my 26 arrays? so even if I say: x-5+3=y =? x-2=y it is the same like x+24=y ? isnt it? Please dont bother telling me it might be +25 or +23 and that I am not going to have 24 arrays - its 8am and I didnt sleep - I am just asking about the principle - I dont want you to do my work - I am just looking for some comfirmation and an idea.


回答1:


$chars = range('a', 'z');
$shift = 5;
$shifted = array_merge(array_slice($chars, $shift), array_slice($chars, 0, $shift));
$alphabet = array_combine($chars, $shifted);

Since there are 26 characters in your alphabet you can only shift them by 26 characters, meaning there are 26 possible combinations.



来源:https://stackoverflow.com/questions/5854291/php-dynamic-creation-of-alphabet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!