Randomize text letters while keeping the words readable
In 29 May 1999, Graham Rawlinson wrote a letter to New Scientist, commenting about a research made by 2 scientists: Kourosh Saberi and David Perrott. Here’s an excerpt from that letter:
You report that reversing 50-millisecond segments of recorded sound does not greatly affect listeners’ ability to understand speech (In Brief, 1 May, p 27). This reminds me of my PhD at Nottingham University (1976), which showed that randomising letters in the middle of words had little or no effect on the ability of skilled readers to understand the text. Indeed one rapid reader noticed only four or five errors in an A4 page of muddled text.
Here’s a function that converts a normal text into a scrambled one, but still readable:
/*
Credits: Bit Repository
URL: http://www.bitrepository.com/web-programming/php/randomize-middle-letters-keeping-the-word-readable.html
*/
function scramble_text($text)
{
$text = ereg_replace('([^A-Za-z0-9])', " \\1", $text);
$keywords = preg_split("/ /", $text);
preg_match_all("/([A-Za-z]{4,})/", $text, $match);
foreach($match[0] as $value)
{
if(in_array($value, $keywords))
{
/* [NEW SCRAMBLED WORD BEGIN] */
$new_scrambled_word = $value[0];
/* Middle Letters Scramble */
$middle_letters = substr($value, 1, -1);
if(strlen($middle_letters) == 2)
{
$new_scrambled_word .= $middle_letters[strlen($middle_letters) - 1].$middle_letters[0];
}
else
{
$chars = preg_split('//', $middle_letters, -1, PREG_SPLIT_NO_EMPTY);
shuffle($chars);
foreach($chars as $char)
{
$new_scrambled_word .= $char;
}
}
$new_scrambled_word .= $value[strlen($value) - 1];
/* [NEW SCRAMBLED WORD END] */
$key = array_search($value, $keywords);
$keywords[$key] = $new_scrambled_word;
}
}
/* The $keywords array now contains the scrambled words */
$new_text = '';
foreach($keywords as $value)
{
$new_text .= ($value) ? $value : ' ';
}
$new_text = trim($new_text);
return $new_text;
}
$text = 'The goal of the PHP language is to allow web developers to write dynamically generated pages quickly.';
$scrambled_and_readable = scramble_text($text);
echo $scrambled_and_readable;
/*
Possible output:
The gaol of the PHP lguanage is, to aollw web dpleeovers to wirte dmncalyaily gaeenertd peags qcikuly.
*/
The archive is made using WinZip 12.0. If you're having problems unzipping it, consider using WinRar, WinAce or a similar software to extract the files from the archive.Be notified when we have new posts by subscribing to


You should use string indexing with the curly bracket like you did on line 20, due to it being removed in PHP 6. Use square brackets insted.
Sorry, I ment you SHOULDN’T
OK, thanks. I choose the curly braces because I’m very used with them. I will update the post. Will use the square brackets instead.