PHP Rot47 Function (str_rot47)


PHP Provides str_rot13 function to rotate alphabetic characters by 13 places. However, there is no str_rot47 function to rotate 47 places over a larger mapping table. The following is a quick rot47 function add-on you can easily use in your PHP application. The quick implementation is possible by using PHP strstr function that practically-fast maps a set of characters to another set.

This previous post talks about rotating functions implementation in batch.

1
2
3
4
5
6
7
8
9
  
if (!function_exists('str_rot47')) {
  function str_rot47($str) {
    return strtr($str, 
      '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 
      'PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO'
    );
  }
}
  
if (!function_exists('str_rot47')) {
  function str_rot47($str) {
    return strtr($str, 
      '!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~', 
      'PQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO'
    );
  }
}

You could refer to this site for more rot47 implementations. You could also write a function to invoke the API like this:

1
2
3
4
function remote_str_rot47($str) {
    $data = file_get_contents("https://helloacm.com/api/rot47/?s=$str");
    return json_decode($data, true);
}
function remote_str_rot47($str) {
    $data = file_get_contents("https://helloacm.com/api/rot47/?s=$str");
    return json_decode($data, true);
}

ROT47 Online Encoder & Decoder

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
309 words
Last Post: Longest Increasing Sequence
Next Post: WordPress URL Rewrite Rules Explained

The Permanent URL is: PHP Rot47 Function (str_rot47)

Leave a Reply