PHP: Randomize Text Letters while Keeping the Words Readable
Posted on October 4, 2008, under PHP,
Bookmark it
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.
Aoccdrnig to rscheearch at an Elingsh uinervtisy, it deosn’t mttaer in waht oredr the ltteers in a wrod are, olny taht the frist and lsat ltteres are at the rghit pcleas. 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 ilstef, but the wrod as a wlohe.
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.
*/
- October 4, 2008
- article by Gabriel C.
- 3 comments
Related Posts
-
How to Create an Advanced PHP (bad, naughty) Words Filterat October 26, 2008 with 8 comments
-
PHP: Get Main Base URLat September 8, 2008 with 5 comments
-
How to emphasize specific words from a string (text)at September 6, 2008
-
Highlight (Search) Key Words in a Text | PHPat August 30, 2008 with 16 comments
-
Shorten a string (text)at September 3, 2008 with 2 comments


Comment via Facebook
3 Replies to "PHP: Randomize Text Letters while Keeping the Words Readable"
October 8, 2008 at 10:08 AM
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.
October 8, 2008 at 10:08 AM
Sorry, I ment you SHOULDN’T
October 8, 2008 at 10:18 AM
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.