Jump to content

Translator


JenL

Recommended Posts

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

 

Link to comment
Share on other sites

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!');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.