Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I Replace places in string with a random value from an array

    So I have a string and an array. Example: $array = array("narwhal","bacon","rage","fffffffuuuuuuuuuuu");

    $string = "I love object, Can't get enough of object, and am addicted to object"

    What I want to do is re-write the string, replacing every time there is object with another random thing from the array.. So the string will look like:

    I love bacon, can't get enough rage, and am addicted to fffffffuuuuuuuuuuuu , everytime the script is loaded it can vary.

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    PHP Code:
    $string "I love object, Can't get enough of object, and am addicted to object";
    $array = array("narwhal","bacon","rage","fffffffuuuuuuuuuuu");
    $new_string str_replace("object",$array[array_rand($array)],$string); 
    Untested, but this should work (Atleast for 1 'object')
    Last edited by Wanna; 09-14-2011 at 09:43 AM.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Quote Originally Posted by Wanna View Post
    PHP Code:
    $string "I love object, Can't get enough of object, and am addicted to object";
    $array = array("narwhal","bacon","rage","fffffffuuuuuuuuuuu");
    $new_string str_replace("object",$array[array_rand($array)],$string); 
    Untested, but this should work (Atleast for 1 'object')
    That wont work. What your code will do, is replace all occurrences of object with the 1 value chosen at random from the array. What your code will give, is something similar to "I love bacon, Can't get enough of bacon, and am addicted to bacon".
    What you need, is a recursive solution. Recursion is the answer here, especially when you can have an infinite number of 'object' string matches. Here's a function I made right now (tested):
    PHP Code:
    function replace_object($string,$array,$count){
        
    // Find the next occurrence of 'object'
        
    $pos strpos($string'object');
        
    // If there was an occurrence
        
    if($pos !== FALSE){
            
    // Replace that string using a substr before the o in object, and after the t in object
            
    $string substr($string,0,$pos).$array[rand(0,$count)].substr($string,($pos+6));
            
    // Recur function to check for more occurrences of object
            
    return replace_object($string,$array,$count);
        }
        else{
            
    // No more object strings, return the string as it is, killing the recursion
            
    return $string;
        }
    }
    $array = array("narwhal","bacon","rage","fffffffuuuuuuuuuuu");
    // Work out length of array, -1 for using as index
    $count count($array)-1;
    $string "I love object, Can't get enough of object, and am addicted to object";
    echo 
    replace_object($string,$array,$count); 

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Here is an alternative using preg_replace_callback(), just becase :
    PHP Code:
    $string "I love object, Can't get enough of object, and am addicted to object"

    function 
    pick_replacement($input)
    {
        
    $myReplacements = array('narwhal''bacon''rage''fffffffuuuuuuuuuuu');
        return 
    $myReplacements[array_rand($myReplacements)];
    }

    echo 
    preg_replace_callback('/object/''pick_replacement'$string); 

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Another solution for the H-E-double-hockey-sticks of it. Let's take the preg_replace_callback() solution one step further and eliminate the callback:
    PHP Code:
    $string 'I love object, Can\'t get enough of object, and am addicted to object';
    $array  = array( 'narwhal''bacon''rage''fffffffuuuuuuuuuuu' );

    echo 
    preg_replace'/object/e''$array[ array_rand( $array ) ]'$string ); 
    If you want to use each value only once, make sure the item count in $array is >= the instances of the word you're replacing:
    PHP Code:
    $string 'I love object, Can\'t get enough of object, and am addicted to object';
    $array  = array( 'narwhal''bacon''rage''fffffffuuuuuuuuuuu' );

    shuffle$array );

    echo 
    preg_replace'/object/e''array_shift( $array )'$string ); 
    Using the preg_replace() or preg_replace_callback() solutions, you can also add the `i` modifier to make the replacements case-insensitive.
    Last edited by kbluhm; 09-14-2011 at 10:40 PM.

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Pffft. Doesn't randomize each time. Weakkk.

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    D'oh!

  • #8
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    Lol :P. I like your method Inigoesdr. Forgot about the callback!


  •  

    Posting Permissions

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