Richard
Richard

Reputation: 1590

PHP dynamic creation of alphabet

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.

Upvotes: 0

Views: 1070

Answers (1)

deceze
deceze

Reputation: 522342

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

Upvotes: 1

Related Questions