Canaan Posted March 2, 2017 Share Posted March 2, 2017 Hi, all I'm new here so not sure if i'm doing this right but i was hoping to create my own form of PHP based encryption function. I'm sure there are already functions out there but i wanted to see if this was something i could do personally. So my situation is i have used the str_replace function to change the inputted letters to numbers then explode the string to an array. Then I add on however many in the alphabet i would like to move the inputted string down the alphabet by then this array is simply imploded and re-written back into the alphabet. This issue i have is as some might have already realized yet i find it hard to explain so i have to give an example, so say i have abcdefgh and i want to move it down 4 this would first turn into 1,2,3,4,5,6,7,8 then i would add on 4 to each number so this would make 5,6,7,8,9,10,11,12 then as i try to change this back to letters anything above 9 is read as separate number so i would get this changed back to e,f,g,h,i,a,0,a,a,a,b. I know that it recognizes the double digit numbers as two separate numbers but i'm not sure how i could get past this. below is the code and yes before anyone says it, i' m sure there's an easier way but i only clear up my code once i get the outcome i want. Thank-you for reading this, Canaan. <?php function encrypt($string, $multiplyer) { $string = preg_replace('/\s+/', '', $string); $legnthofstring = strlen($string); $string = strtolower($string); $string = str_replace("a","1",$string); $string = str_replace("b","2",$string); $string = str_replace("c","3",$string); $string = str_replace("d","4",$string); $string = str_replace("e","5",$string); $string = str_replace("f","6",$string); $string = str_replace("g","7",$string); $string = str_replace("h","8",$string); $string = str_replace("i","9",$string); $string = str_replace("j","10",$string); $string = str_replace("k","11",$string); $string = str_replace("l","12",$string); $string = str_replace("m","13",$string); $string = str_replace("n","14",$string); $string = str_replace("o","15",$string); $string = str_replace("p","16",$string); $string = str_replace("q","17",$string); $string = str_replace("r","18",$string); $string = str_replace("s","19",$string); $string = str_replace("t","20",$string); $string = str_replace("u","21",$string); $string = str_replace("v","22",$string); $string = str_replace("w","23",$string); $string = str_replace("x","24",$string); $string = str_replace("y","25",$string); $string = str_replace("z","26",$string); $arr1 = str_split($string); $counter = 0; $los=$legnthofstring; while($counter < $los) { $arr1[$counter] += $multiplyer; $counter ++; } $tring = implode(",", $arr1); echo($tring); echo"<p>"; $tring = str_replace("1","a",$tring); $tring = str_replace("2","b",$tring); $tring = str_replace("3","c",$tring); $tring = str_replace("4","d",$tring); $tring = str_replace("5","e",$tring); $tring = str_replace("6","f",$tring); $tring = str_replace("7","g",$tring); $tring = str_replace("8","h",$tring); $tring = str_replace("9","i",$tring); $tring = str_replace("10","j",$tring); $tring = str_replace("11","k",$tring); $tring = str_replace("12","l",$tring); $tring = str_replace("13","m",$tring); $tring = str_replace("14","n",$tring); $tring = str_replace("15","o",$tring); $tring = str_replace("16","p",$tring); $tring = str_replace("17","q",$tring); $tring = str_replace("18","r",$tring); $tring = str_replace("19","s",$tring); $tring = str_replace("20","t",$tring); $tring = str_replace("21","u",$tring); $tring = str_replace("22","v",$tring); $tring = str_replace("23","w",$tring); $tring = str_replace("24","x",$tring); $tring = str_replace("25","y",$tring); $tring = str_replace("26","z",$tring); echo $tring; } encrypt("abcdefg",3); Quote Link to comment https://forums.phpfreaks.com/topic/303332-alphabetic-re-arrangement-of-strings/ Share on other sites More sharing options...
Jacques1 Posted March 2, 2017 Share Posted March 2, 2017 (edited) You should actually clean up your code right now, because this is far too complicated. The entire procedure can be done in one run: You iterate over each character, get its numeric value, increase the value and then write the new character back into a string. As pseudo code: encrypt_caesar(plaintext, offset): ciphertext = "" for each char in plaintext: // convert character to number char_number = char_to_numeric(char) // add offset to number; warning: the result can get bigger than 26, so you need to wrap it (using the modulo operation) char_number = char_number + offset // convert number back to character and append that character to the ciphertext chipertext = ciphertext + numeric_to_char(char_number) return ciphertext See ord() and chr() for conversions between characters and numbers. Edited March 2, 2017 by Jacques1 Quote Link to comment https://forums.phpfreaks.com/topic/303332-alphabetic-re-arrangement-of-strings/#findComment-1543617 Share on other sites More sharing options...
requinix Posted March 2, 2017 Share Posted March 2, 2017 I'm new here so not sure if i'm doing this right but i was hoping to create my own form of PHP based encryption function.As long as you're doing this for fun and not for real encryption then, Are you trying to turn "abcdefgh" into "efghijkl"? By adding 4 to each letter? strtr can replace multiple characters at once. To do +4 you could use it like strtr("abcdefgh", "abcdefgh", "efghijkl") // efghijkl // that's not a good example of how it works, so strtr("abcdefgh", "ghefcdab", "klijghef") // g->k, h->l, etc. The second argument to "search" for is just the alphabet, so you need to come up with the third. Start with "abc...xyz" then use string functions like substr to rearrange it so that abcdefghijklmnopqrstuvwxyz /\ 4 abcd efghijklmnopqrstuvwxyz efghijklmnopqrstuvwxyz abcd efghijklmnopqrstuvwxyzabcd Quote Link to comment https://forums.phpfreaks.com/topic/303332-alphabetic-re-arrangement-of-strings/#findComment-1543618 Share on other sites More sharing options...
Canaan Posted March 2, 2017 Author Share Posted March 2, 2017 yeah thanks for the help I've only been using PHP for like a month so i'm just testing what i can do not for real use i know that it would be much more secure to use a hash but i just found it interesting and i'm not really sure substr as I've only learned the basics and pretty much stumbled on the idea but it was more the thought of a procedural number but i just found it an interesting idea thanks for the quick responses. Quote Link to comment https://forums.phpfreaks.com/topic/303332-alphabetic-re-arrangement-of-strings/#findComment-1543620 Share on other sites More sharing options...
requinix Posted March 2, 2017 Share Posted March 2, 2017 substr is one of the most basic string functions PHP has so go ahead and start learning about it now - you were using str_replace just fine (except for the repetition, that is). Quote Link to comment https://forums.phpfreaks.com/topic/303332-alphabetic-re-arrangement-of-strings/#findComment-1543634 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.