Letter Scrambling Practical Joke

By now, nearly everyone has seen the widespread message that reads:

Aoccdrnig to a rsecheearr at an Elingsh uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is that frist and lsat ltteer is at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae we do not raed ervey lteter by itslef but the wrod as a wlohe.

Translation:

According to a researcher at an English university, it doesn't matter in what order the letters in a word are, the only important thing is that first and last letter is at the right place. The rest can be a total mess and you can still read it without a problem. This is because we do not read every letter by itself but the word as a whole.

The Scenario

As a practical joke for one of my clients, I decided to make a total mess out of their website using this tactic. Because I didn't want to spend a lot of time doing this and then fixing it again, I wrote a simple little function to help out.

The website in question got all it's text from a database and displayed that for the web visitor. This made the task quite easy to do and reverse! I simply added an extra function call to the output before displaying it (a single line) and the comment it out to "fix" the site the way it was.

The Method

To see what this does, enter your sentance and check out the result!


PHP Source Code for the Letter Scrambler

<?php
    
function wordScramble($x){
        
// split up the words and puctuation
        
$ar=preg_split('/\b/',$x);
        
        
// delete empty elements
        
foreach($ar as $y=>$z)
            if(empty(
$z))
                unset(
$ar[$y]);
        
        
$output=array();
        foreach(
$ar as $word){
            
$elem=count($output)-1;
            if(
strlen($word)>3){
                
$word_start=substr($word,0,1);  // first letter
                
$word_end=substr($word,-1);     // last letter
                
$word_rest=substr($word,1,-1);  //rest of the word
                
$word_rest_ar=array();
                for(
$i=0;$i<strlen($word_rest);$i++){
                    
// for each letter for the rest of the word
                    
$word_rest_ar[]=substr($word_rest,$i,1);
                }
                
shuffle($word_rest_ar);
                
$word=$word_start.join('',$word_rest_ar).$word_end;
            }
            
$output[]=$word;
        }
        
$find=array(" ' ",'( ',' )','{ ',' }','[ ',' ]',' .',' ?',' !',' , ');
        
$replace=array("'",'(',')','{','}','[',']','.','?','!',', ');
        return
preg_replace('/(\r\n|\r|\n){2}/',"\n",str_replace($find,$replace,join(' ',$output)));
    }
?>