PHP Implementing Phrase-Substitution Algorithm – String Function

<?php
  $codebook = array(
    'A' => 'a',
    'sun' => 'frog',
    'rises' => 'jumps'
  );
  $message = 'The sun rises.';
  $encoded_message = str_ireplace(array_keys($codebook), array_values($codebook),
                                  $message);
  $decoded_message = str_ireplace(array_values($codebook), array_keys($codebook),
                                  $encoded_message);
?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.