The String Mangler

This program is designed to mangle strings - type your string into the box and see what happens to it.

 

Code:

<h3>The String Mangler</h3>
<p>This program is designed to mangle strings - type your string into the box and see what happens to it.</p>
<form name="mangle" method="post" action="index.php">
<?php
   // starting state variables
   if (isset($_POST['submit'])) {
?>

<input type="text" name="source" value="<?php echo $_POST['source']?>" />
<input type="submit" name="submit" value="mangle"  />
</form>

<?php
      //retrieve message to be mangled
      $msg = $_POST['source'];
   
      // output message pre-mangle
      echo "<br>Your message was: " . $msg ;
   
      // work out the length of the string
      $long = strlen($msg); 
      echo "<br>Your message had " . $long . " characters in it";
   
      //work out the number of words in it
      $wordcount = 1;
      for ($k=1; $k<$long; $k++)
            if ($msg[$k] == " ")
               $wordcount++;
      echo "<br>The message contains " . $wordcount . " words";
   
      //mix it up
      echo "<br>Your message jumbled is " . str_shuffle($msg); 
   
      //reverse it
      $reverse = "";
      for ($i=strlen($msg)-1; $i>=0; $i--) 
            $reverse = $reverse . $msg[$i];  // $reverse .= $msg[$i];
      echo "<br>Your message in Reverse = " . stripslashes($reverse);
   
      //acronym it
      $acro = "";
      $copy = ltrim(ucwords($msg));  //ltrim exterminates leading spaces
                                     //ucwords uppercases the first letter of each word 
      $acro = $copy[0];
      $j = 2;
      while ($j < $long) {
            if ($copy[$j] == " ")
               $acro = $acro . $copy[$j+1];  // $acro .=$copy[$j+1];
            $j++;
      }
      echo "<br>Your message as an Acronym is " . $acro;
 
   } else {
?>

<input type="text" name="source" value="type your message here" />
<input type="submit" name="submit" value="mangle"  />
</form>

<?php
      $msg = "";   //the message yet to be mangled
   }

Variations

   //thanks paul
   //work out the number of words in it
   $words = split(' ', $msg);
   printf("<br>The message contains %s words", count($words));
   
   and then using the same array
   
   //acronym it
   foreach ($words as $word) {
   $acro .= $word[0];
   }
   echo "<br>Your message as an Acronym is " . strtoupper($acro);