Scrambling Letters in A Variable

Discussion in 'PHP' started by qdsouza, Sep 5, 2005.

  1. #1
    Is there a simple way to scramble letters in a variable?

    If I have something like:

    $word='animal'

    and I get something like the:

    $word1='minala"

    Thanks
     
    qdsouza, Sep 5, 2005 IP
  2. LGRComp

    LGRComp Well-Known Member

    Messages:
    516
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    195
    #2
    str_shuffle() should do what you want.

    See: http://ca.php.net/manual/en/function.str-shuffle.php
     
    LGRComp, Sep 5, 2005 IP
    sarahk likes this.
  3. sarahk

    sarahk iTamer Staff

    Messages:
    28,930
    Likes Received:
    4,563
    Best Answers:
    124
    Trophy Points:
    665
    #3
    probably not.

    but this will do it
    <?PHP
    /**
     * @return string
     * @param string $word
     * @desc scrambles the word given.
    */
    function scramble($word)
    {
    	$newword = array();
    	for($i = 0; $i < strlen($word); $i++)
    	{
    		$num = rand(0, 100);
    		$newword[$num] = substr($str, $i, 1);
    	}
    	ksort($newword);
    	$output = implode('', $newword);
    	return $output;
    }
    
    echo scramble('animal');
    ?>
    Code (markup):
     
    sarahk, Sep 5, 2005 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,930
    Likes Received:
    4,563
    Best Answers:
    124
    Trophy Points:
    665
    #4
    good find LGRComp

    I didn't even check to see if there was a function!
     
    sarahk, Sep 5, 2005 IP
  5. qdsouza

    qdsouza Guest

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the help that did it :)
     
    qdsouza, Sep 5, 2005 IP