Randomising The Middle Of Words In PHP

18th November 2008

I was sent an email the other day that contained some text were the start and end letter of each word were left alone, but the middle of each word was randomized. The weird part was that the text was still readable, which is due to the way in which the brain processes words.

I wondered if I could replicate this using a PHP script. All I would need to do is split apart the sentence into the component words and loop through those words, randomizing the middle of them. Clearly, it is not possible to mix up the order of letters in a word less than four characters long so a check would be needed for this. This is what I cam up with:

  1. function mixWordMiddle($string)
  2. {
  3. $string = explode(' ',$string);
  4. foreach ( $string as $pos => $word ) {
  5. $tmpArray = array();
  6. if ( strlen($word) > 3 ) {
  7. $chars = preg_split('//', $word, -1, PREG_SPLIT_NO_EMPTY);
  8. for ( $i = 1 ; $i
  9.  
  10. I then tried plugging in the following text about evolution.
  11.  
  12. $string = 'In biology, evolution is the changes in the inherited traits of a population of organisms from one generation to the next. These changes are caused by a combination of three main processes: variation, reproduction, and selection.';
  13.  
  14. And came up with something like the following.
  15.  
  16. In bliygoo, eoutivoln is the cganhes in the iethirned titras of a piaplouotn of oargnsims form one gneoeatirn to the nxte. Thsee cagnhes are ceusad by a cmibitoonan of there main persocses: voaitanri, rteunodpoirc, and stoneleic.
  17.  
  18. Which is actually quite difficult to read. I thought that this might be because I had used a bit of text with too many long words, so I selected another:
  19.  
  20. $string = 'A giant Saudi oil tanker seized by pirates in the Indian Ocean is nearing the coast of Somalia, the US Navy says.';
  21.  
  22. This produced the following text.
  23.  
  24. A ganit Suadi oil taeknr seezid by ptaiers in the Ianidn Oecan is nraneig the cosat of Smiolaa, the US Navy syas.
  25.  
  26. This is just a test script, so it doesn't take into account any punctuation. However, the text it produces is still difficult to read, which leads me be skeptical of the claims of that the email I received.

Add new comment

The content of this field is kept private and will not be shown publicly.