Letter Scrambling Practical Joke

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 then removed it to "fix" the site the way it was.

Example

An example of the function would be to input the following:

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 function would then process and output something like:

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.

Simple. Effective. Fun.

The Code

<?php
/*
KOIVI Word Scrambler for PHP Copyright (C) 2004 Justin Koivisto
Version 2.0
Last Modified: 12/9/2004

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or (at
    your option) any later version.

    This library is distributed in the hope that it will be useful, but
    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this library; if not, write to the Free Software Foundation,
    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

    Full license agreement notice can be found in the LICENSE file contained
    within this distribution package.

    Justin Koivisto
    justin.koivisto@gmail.com
    http://koivi.com
*/

    
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)));
    }

?>