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.
*/

Stay in touch
Sign up for free updates

Related Posts

Comment via Facebook

3 Replies to "PHP: Randomize Text Letters while Keeping the Words Readable"

  1. 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.

  2. Sorry, I ment you SHOULDN’T

  3. 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.

Leave a Reply


* = required fields

  (will not be published)


XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: If you want to post CODE Snippets, please make them postable first!
(e.g. <br /> should be converted to &lt;br /&gt;)