Advertisement
relax4o

Rot13

Aug 2nd, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.87 KB | None | 0 0
  1. function RotThirteen($input)
  2. {
  3.     $allowed_chars = array_merge(range('a','z'), range('A','Z'));
  4.     array_push($allowed_chars, " ");
  5.  
  6.     $output = "";
  7.     for($i = 0; $i < strlen($input); $i++) {
  8.         $current_character = $input[$i];
  9.  
  10.         if ( !in_array($current_character, $allowed_chars) ) {
  11.             continue;
  12.         }
  13.  
  14.         $ascii_code = ord($current_character);
  15.         $rot13_code = $ascii_code + 13;
  16.  
  17.         $substitute_character = "";
  18.         if ( $current_character == " " ) {
  19.             $substitute_character = $current_character;
  20.         } else {
  21.             if ( $rot13_code > 122 ) {
  22.                 $rot13_code = 97 + ($rot13_code - 123);
  23.             } else if ( $rot13_code > 90 && $rot13_code < 104) {
  24.                 $rot13_code = 65 + ($rot13_code - 91);
  25.             }
  26.  
  27.             $substitute_character = chr($rot13_code);
  28.         }    
  29.        
  30.         $output .= $substitute_character;
  31.     }
  32.    
  33.     return $output;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement