Robson » Code » PHP » Rot13

 
<?
   // two rot13 ('rotate alphabet 13 places') decryption/encryption functions
   // rot13 is intended to be a simple encryption method
 
   // --- FIRST METHOD --- //    
   
   // rot 13 using some simple string transalting
   function rot13_1($string)
   {    
       // turn the first characters into the second ones
       $before = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
       $after  = 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM';
       // replace and return
       return strtr($string, $before, $after);
   }
   
   // --- SECOND METHOD --- //
   
   // rot13 using some slightly more advanced character shifting    
   function rot13_2($string)
   {    
       // loop through all the characters
       for ($n = 0; $n < strlen($string); $n++)
       {
           // check if it's at the start of the alphabet
           if (eregi('[a-mA-M]', $string[$n]))
               // if so, add characters to shift it thirteen places forward
               $string[$n] = chr(ord($string[$n]) + 13);
           // if not
           else if (eregi('[n-zN-Z]', $string[$n]))
               // shift it thirteen places back
               $string[$n] = chr(ord($string[$n]) - 13);
       }
       // return the new string
       return $string;
   }
?>
 
© 2004-17 robson | cc unless stated