This is a discussion on Multiple words str_shuffle within the PHP General forums, part of the PHP Programming Forums category; I am trying to scramble individual words and/or phrases. When it is a phrase I would like to keep ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. An example: rise and shine Desired output: I S R E N A D E H I S N Thanks for your help, Ron $keyword might be $keyword = str_shuffle(strtoupper($keyword)); $buffer = ""; for ($count = 0; ($count < strlen($keyword)); $count++) $buffer .= $keyword{$count}." "; $keyword = trim($buffer); unset($buffer); |
|
|||
|
On 7/6/08, Ron Piggott <ron.php@actsministries.org> 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. > > > An example: > > rise and shine > > Desired output: > > I S R E N A D E H I S N > > Thanks for your help, > > Ron > > > > $keyword might be > > $keyword = str_shuffle(strtoupper($keyword)); > > $buffer = ""; > > for ($count = 0; ($count < strlen($keyword)); $count++) $buffer .= > $keyword{$count}." "; > > $keyword = trim($buffer); > > unset($buffer); Once the individual words have had their letters shuffled, explode the sentence on a space, then use the shuffle function ( http://us3.php.net/manual/en/function.shuffle.php) to, um, shuffle the array. David |
|
|||
|
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 |
|
|||
|
On Sun, Jul 6, 2008 at 8:04 PM, Brady Mitchell <mydarb@gmail.com> 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 |
|
|||
|
grrr ... I rear my ugly head, briefly ...
Ron Piggott schreef: > I am trying to scramble individual words and/or phrases. try harder. <?php function mixit($m) { return trim(chunk_split(str_shuffle(strtoupper($m[1])),1,' ')); } echo preg_replace_callback('#(\w+)#', 'mixit', 'The rain. in Spain falls, mainly "on" the plain!'); ?> have integrity, read these before you copy, paste 'n' use: http://php.net/chunk_split http://php.net/preg_replace_callback questions on a postcard to Dan Brown, he just got married, he deserves it ;-) |
|
|||
|
Jochem Maas schreef:
</snip> this is a little better: <?php // $argv[1] is the first script argument on the CLI $phrase = isset($argv[1]) && is_string($argv[1]) ? $argv[1] : 'The rain. in Spain falls, mainly "on" the plain!'; $phrase = preg_replace_callback('#(\w+)#', 'mixit', str_replace(" ", " ", $phrase)); function mixit($m) { return trim(chunk_split(str_shuffle(strtoupper($m[1])),1,' ')); } |
|
|||
|
On Mon, Jul 7, 2008 at 10:44 PM, Jochem Maas <jochem@iamjochem.com> wrote:
> grrr ... I rear my ugly head, briefly ... [snip!] > > questions on a postcard to Dan Brown, he just got married, he deserves it > ;-) Welcome back, brother. Thanks for the well-wishes. I hope you and your son are doing well! -- </Daniel P. Brown> Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just $59.99/mo. with no contract! Dedicated servers, VPS, and hosting from $2.50/mo. |