SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Randomizing a string

    Is there a built in function in php that could randomize a string? For example I have a string:

    $string = 'This is a string';

    I want to randomize it to show something like this:

    this a string is
    a string is this
    string is this a

    Is there a way of doing this? I know there is a string shuffle command but it shuffles at the character level and not the word level. Or do I have to use something like explode and come up with an algorithm to randomize it?

    Thanks
    Silly

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,029
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You could turn your string into an array delimited by spaces so each index is a word then shuffle the array.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  3. #3
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have done that, but I cannot have the same string repeated. I broke it up, put it back to gether as a string, did an array_search and it still gave me duplicate entries. The shuffle doesnt work very well either. Out of 10 it gave me the same string 6 times!

    Silly

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Oct 2001
    Location
    Vancouver BC Canada
    Posts
    2,029
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    You did something like this?

    PHP Code:
    <?php

    $string 
    "this is a string";

    $arraystring explode(" "$string);

    $size count($arraystring);

    shuffle($arraystring);


    for(
    $x=0$x<$size$x++)
    {
        echo 
    $arraystring[$x] . " ";
    }


    ?>
    If you can't have duplicates, you're going to have to add something to validate each string against the others. I've not found that the random and shuffle functions in any of the languages are particularily stellar in not producing duplicates.
    Andrew Wasson | www.lunadesign.org
    Principal / Internet Development

  5. #5
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this:

    PHP Code:

    $string 
    'This is a string';
    $word_array = array();

    $string_array explode(' ',$string);

    while(++
    $i <= 10)
    {

    shuffle($string_array);

    while(list(
    $key,$value) = each($string_array))
    {

    $rebuild_string .= ' ' .$value;

    }

    if(!
    array_search(trim($rebuild_string),$word_array))
    {

    $word_array[] = $rebuild_string;

    }


    Something like that.

    Silly

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you're looking for is called a 'permutation'. Permutation generation algorithm is well known, I'm sure you'll find it...

  7. #7
    SitePoint Wizard mark_W's Avatar
    Join Date
    Mar 2004
    Location
    West Midlands, United Kingdom
    Posts
    2,631
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Im not sure but this class may help you Silly.

    http://vextron.mirrors.phpclasses.or...ckage/200.html

    Mark.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •