Ellingsen Posted July 19, 2008 Share Posted July 19, 2008 Hi guys. I've looking for a way to make my own simple encrypt/decrypt function for my site. Is just a replace "a" with "b" sort of crypt, and i know its a security risk. Its not for any important data But my question is: is there any way to str_replace all characters at one time? if i have a sentence, example sbsbsb, and the code is: $phrase = "ababab"; $from = array("a", "b"); $too = array("b", "a"); $newphrase = str_replace($from, $too, $phrase); RESULT: aaaaaa and i want: bababa Any suggestions? =) Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/ Share on other sites More sharing options...
DarkWater Posted July 19, 2008 Share Posted July 19, 2008 Try something like: <?php function encrypt($string) { $find = array_merge(range('a', 'z'), range('A', 'Z')); $replace = array ( 0 => 'R', 1 => 'x', 2 => 'q', 3 => 'i', 4 => 'J', 5 => 'j', 6 => 'Z', 7 => 'e', 8 => 'P', 9 => 'c', 10 => 'L', 11 => 'v', 12 => 'm', 13 => 'B', 14 => 'V', 15 => 'D', 16 => 'w', 17 => 'f', 18 => 'Y', 19 => 's', 20 => 'O', 21 => 'M', 22 => 'E', 23 => 'y', 24 => 'a', 25 => 'g', 26 => 'I', 27 => 'u', 28 => 'p', 29 => 'G', 30 => 'C', 31 => 'K', 32 => 't', 33 => 'n', 34 => 'r', 35 => 'X', 36 => 'l', 37 => 'Q', 38 => 'S', 39 => 'b', 40 => 'k', 41 => 'F', 42 => 'A', 43 => 'd', 44 => 'W', 45 => 'U', 46 => 'h', 47 => 'T', 48 => 'N', 49 => 'H', 50 => 'o', 51 => 'z', ); $string = str_replace($find, $replace, $string); return $string; } That $replace array was made with: <?php $let = range('a', 'z'); $uplet = range('A', 'Z'); $merge = array_merge($let, $uplet); shuffle($merge); var_export($merge); ?> Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594461 Share on other sites More sharing options...
MadTechie Posted July 20, 2008 Share Posted July 20, 2008 you can't really do it as one massive replace i would probably do something like this <?php $phrase = "abababDbbb"; $crypt = array( "a" => "b", "b" => "c", "c" => "a" ); $p = preg_split("//", $phrase); //cleanup unset($p[0]); unset($p[count($p)]); $newphrase = ""; foreach($p as $letter) { if(isset($crypt[$letter])) { $newphrase .= $crypt[$letter]; }else{ $newphrase .= $letter; } } echo $newphrase; ?> Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594571 Share on other sites More sharing options...
unkwntech Posted July 20, 2008 Share Posted July 20, 2008 preg_replace is a bit slower however it will accept arrays This is a small script I have that does something similar maybe you can get an idea. <?php /** * @author [email protected] * @copyright 2008 **/ if($_GET['act'] == 'do') { $pattern['a'] = '/[a]/'; $replace['a'] = '[a A @]'; $pattern['b'] = '/[b]/'; $replace['b'] = '[b B I3 l3 i3]'; $pattern['c'] = '/[c]/'; $replace['c'] = '(?:[c C (]|[k K])'; $pattern['d'] = '/[d]/'; $replace['d'] = '[d D]'; $pattern['e'] = '/[e]/'; $replace['e'] = '[e E 3]'; $pattern['f'] = '/[f]/'; $replace['f'] = '(?:[f F]|[ph pH Ph PH])'; $pattern['g'] = '/[g]/'; $replace['g'] = '[g G]'; $pattern['h'] = '/[h]/'; $replace['h'] = '[h H]'; $pattern['i'] = '/[i]/'; $replace['i'] = '[i I l ! 1]'; $pattern['j'] = '/[j]/'; $replace['j'] = '[j J]'; $pattern['k'] = '/[k]/'; $replace['k'] = '(?:[c C (]|[k K])'; $pattern['l'] = '/[l]/'; $replace['l'] = '[l L 1 ! i]'; $pattern['m'] = '/[m]/'; $replace['m'] = '[m M]'; $pattern['n'] = '/[n]/'; $replace['n'] = '[n N]'; $pattern['o'] = '/[o]/'; $replace['o'] = '[o O 0]'; $pattern['p'] = '/[p]/'; $replace['p'] = '[p P]'; $pattern['q'] = '/[q]/'; $replace['q'] = '[q Q]'; $pattern['r'] = '/[r]/'; $replace['r'] = '[r R]'; $pattern['s'] = '/[s]/'; $replace['s'] = '[s S $ 5]'; $pattern['t'] = '/[t]/'; $replace['t'] = '[t T 7]'; $pattern['u'] = '/[u]/'; $replace['u'] = '[u U v V]'; $pattern['v'] = '/[v]/'; $replace['v'] = '[v V u U]'; $pattern['w'] = '/[w]/'; $replace['w'] = '[w W vv VV uu UU]'; $pattern['x'] = '/[x]/'; $replace['x'] = '[x X]'; $pattern['y'] = '/[y]/'; $replace['y'] = '[y Y]'; $pattern['z'] = '/[z]/'; $replace['z'] = '[z Z 2]'; $word = str_split(strtolower($_POST['word'])); $i=0; while($i < count($word)) { if(!is_numeric($word[$i])) { if($word[$i] != ' ' || count($word[$i]) < '1') { $word[$i] = preg_replace($pattern[$word[$i]], $replace[$word[$i]], $word[$i]); } } $i++; } //$word = "/" . implode('', $word) . "/"; echo implode('', $word); } ?> This is used to generate regex patters from user imput, we use this for admins to insert a 'bad' word into our filter database, it will catch all variations of a word (ie badword = b@duu0rd) Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594726 Share on other sites More sharing options...
Barand Posted July 20, 2008 Share Posted July 20, 2008 strtr() <?php $phrase = "ababab"; $tran = array( "a" => "b", "b" => "a" ); $newphrase = strtr($phrase,$tran); echo $newphrase; // bababa ?> Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594730 Share on other sites More sharing options...
paul2463 Posted July 20, 2008 Share Posted July 20, 2008 <?php function Encrypt($text) { $length=strlen($text); for ($i = 0; $i < $length; $i++) { $char = ord($text[$i]); //gets the ascii character number of the letter $char1 = chr($char +1); //adds 1 onto the ascii charachter a=b f=g etc $output .= $char1; //remakes the string } return $output; } $text = "hello world"; echo Encrypt($text); //prints out ifmmp!xpsme ?> rewrite it slightly to decrypt the line by making it minus1 Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594746 Share on other sites More sharing options...
Ellingsen Posted July 20, 2008 Author Share Posted July 20, 2008 Thank you guys. I've tried Barand's way, but i cant get it to work properly.. <?php $phrase = "test"; $tran = array( "a" => "u", "b" => "v", "c" => "A", "d" => "ø", "e" => "å", "f" => "F", "g" => "B", "h" => "æ", "i" => "7", "j" => "Æ", "k" => "1", "l" => "L", "m" => "E", "n" => "5", "o" => "s", "p" => "8", "q" => "w", "r" => "l", "s" => "3", "t" => "Å", "u" => "Q", "v" => "Y", "w" => "-", "x" => "*", "y" => "S", "z" => "V", "æ" => "!", "ø" => "n", "å" => "y", "A" => "2", "B" => "H", "C" => ",", "D" => "t", "E" => "6", "F" => ".", "G" => "X", "H" => "R", "I" => "P", "J" => "T", "K" => "G", "L" => "K", "M" => "d", "N" => "z", "O" => "r", "P" => "Ø", "Q" => "M", "R" => "h", "S" => ";", "T" => "c", "U" => "?", "V" => "m", "W" => "N", "X" => "J", "Y" => "q", "Z" => "o", "Æ" => "O", "Ø" => "f", "Å" => "0", "1" => "U", "2" => "p", "3" => "j", "4" => "a", "5" => "=", "6" => ":", "7" => "g", "8" => "C", "9" => "_", "0" => "x", "!" => "4", "?" => "I", "=" => "i", "-" => "b", "_" => "k", "*" => "e", "/" => "D", "." => "Z", "," => "W", ":" => "/", ";" => "9" ); $newphrase = strtr($phrase,$tran); echo $newphrase; ?> It outputs this: Ã…Ã¥3Ã… any suggestions? Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594757 Share on other sites More sharing options...
JasonLewis Posted July 20, 2008 Share Posted July 20, 2008 I tried Barand's example and it worked fine for me. Tried a few different strings and they worked perfectly. How did you implement it? Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594758 Share on other sites More sharing options...
Ellingsen Posted July 20, 2008 Author Share Posted July 20, 2008 Well, i just ran the php file on my server. Worked with the script he wrote, but when i extended it into taking alot more characters, it bugged. Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594761 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 could be your charset? are you using utf-8 Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594763 Share on other sites More sharing options...
Ellingsen Posted July 20, 2008 Author Share Posted July 20, 2008 Oh, i sure could be.. and it is! Thank you, just had a plain php file, added the html-charset at the top and it all works fine. thanks alot to all the posters in this topic. Great help. Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594765 Share on other sites More sharing options...
DeanWhitehouse Posted July 20, 2008 Share Posted July 20, 2008 good, click topic solved at the bottom. Link to comment https://forums.phpfreaks.com/topic/115640-solved-question-about-str_replace/#findComment-594769 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.