On Sun, Jul 6, 2008 at 8:04 PM, Brady Mitchell <mydarb@xxxxxxxxx> wrote: > 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 so it will echo and be obvious that there are two spaces > between words. > $scramble_phrase = implode(' ',$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 Why not something like this? <?php $phrase = 'The rain in Spain falls mainly on the plain'; $words = split(' ', $phrase); array_walk($words, 'str_shuffle'); echo join(' ', $words); ?> Andrew -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php