JenL Posted December 30, 2011 Share Posted December 30, 2011 Hee guys, We are building a PHP translater to generate the caesar code. Like A = B B = C etc. We want that other people who visite our website, can choose which rotation they would have. so rot 1 = A + 1, Rot 2 = A + 2 ( A=C), we built the follow code: <?php function translate($string) { $code = array ('a','b','c','d','e','f'); $translate = array ('b','c','d','e','f','g'); return str_replace($code,$translate,$string); } Echo translate('abcdef'); ?> How could you built a code if other people want an other rotation? so they can choose for more then one rotation. Thank you Greats Jeanine en Lotte Quote Link to comment https://forums.phpfreaks.com/topic/254081-translator/ Share on other sites More sharing options...
Muddy_Funster Posted December 30, 2011 Share Posted December 30, 2011 use chr() = chr+$noOfTurns in place of the hard set translate array. you will need to itterate it through the lenth of the $string input using substr. Quote Link to comment https://forums.phpfreaks.com/topic/254081-translator/#findComment-1302550 Share on other sites More sharing options...
ignace Posted December 30, 2011 Share Posted December 30, 2011 Here's a little class I wrote that will do what you want: class CaesarCode { private static $alfabetUpper; private static $alfabetLower; private $steps; private function _getNewPosition($originalPosition) { return ($originalPosition + $this->steps) % 26; } private function _getCharFromArray($char, $fromArray) { if (($charKey = array_search($char, $fromArray)) !== false) { return $fromArray[$this->_getNewPosition($charKey)]; } return false; } private function _translateChar($char) { $translatedChar = $this->_getCharFromArray($char, $this->_getAlfabetUpper()); if ($translatedChar === false) { $translatedChar = $this->_getCharFromArray($char, $this->_getAlfabetLower()); } return $translatedChar; } private function _getAlfabetUpper() { if (empty(self::$alfabetUpper)) { self::$alfabetUpper = range('A', 'Z'); } return self::$alfabetUpper; } private function _getAlfabetLower() { if (empty(self::$alfabetLower)) { self::$alfabetLower = range('a', 'z'); } return self::$alfabetLower; } public function __construct($steps) { $this->steps = $steps; } public function translate($string) { $encr = ''; foreach (str_split($string) as $key => $char) { if (($newChar = $this->_translateChar($char)) !== false) $char = $newChar; $encr .= $char; } return $encr; } } Use it like: $code = new CaesarCode(13); echo $code->translate('Hello World!'); Quote Link to comment https://forums.phpfreaks.com/topic/254081-translator/#findComment-1302623 Share on other sites More sharing options...
JenL Posted January 1, 2012 Author Share Posted January 1, 2012 O cool Really thanks! Quote Link to comment https://forums.phpfreaks.com/topic/254081-translator/#findComment-1303024 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.