Bluehost.com Web Hosting $6.95

Multiple words str_shuffle

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


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-07-2008
Ron Piggott
 
Posts: n/a
Default Multiple words str_shuffle


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);


Reply With Quote
  #2 (permalink)  
Old 07-07-2008
David Giragosian
 
Posts: n/a
Default Re: [PHP] Multiple words str_shuffle

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

Reply With Quote
  #3 (permalink)  
Old 07-07-2008
Brady Mitchell
 
Posts: n/a
Default Re: [PHP] Multiple words str_shuffle

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
Reply With Quote
  #4 (permalink)  
Old 07-07-2008
Andrew Ballard
 
Posts: n/a
Default Re: [PHP] Multiple words str_shuffle

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 &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


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
Reply With Quote
  #5 (permalink)  
Old 07-08-2008
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Multiple words str_shuffle

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 ;-)
Reply With Quote
  #6 (permalink)  
Old 07-08-2008
Jochem Maas
 
Posts: n/a
Default Re: [PHP] Multiple words str_shuffle

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,' '));
}
Reply With Quote
  #7 (permalink)  
Old 07-08-2008
Daniel Brown
 
Posts: n/a
Default Re: [PHP] Multiple words str_shuffle

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.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:12 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0