Re: Multiple words str_shuffle

From: Date: Mon, 07 Jul 2008 00:04:14 +0000
Subject: Re: Multiple words str_shuffle
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-276339@lists.php.net to get a copy of this message
On Jul 6, 2008, at 305PM, Ron Piggott wrote:
I am trying to scramble individual words and/or phrases. When it is a phrase I would like to keep the letters of each word together, with a space between each one. The code I have so far is below. I use PHP 4.4.7. The code below is fine for a single word; it is phrases that I am now trying to accommodate.
$orig_phrase = 'rise and shine'; // Split the phrase into an array with each word as an element $array_phrase = explode(' ',$orig_phrase); // Cycle through the array processing one word at a tie foreach($array_phrase as $key => $value) { // $orig_value is used in the do while loop to ensure that the "shuffled" string is not the original string. $orig_value = $value; // Shuffle the string, and continue to do so until the returned string is not the original string do{ $value = str_shuffle($value); } while($value == $orig_value); // Uppercase value $value = strtoupper($value); // Insert a space after every letter $value = chunk_split($value,1,' '); // Set array value to newly formatted version $array_phrase[$key] = $value; } // I'm using &nbsp; so it will echo and be obvious that there are two spaces between words. $scramble_phrase = implode('&nbsp;&nbsp;',$array_phrase); echo $orig_phrase; echo '<br />'; echo $scramble_phrase; Everything after the do...while loop can be easily combined into one line; I left it as separate lines for clarity. Brady

« previous php.general (#276339) next »