...

View Full Version : How do I Replace places in string with a random value from an array



CyberVillian
09-14-2011, 09:11 AM
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.

Wanna
09-14-2011, 09:41 AM
$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')

BluePanther
09-14-2011, 10:05 AM
$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):


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

Inigoesdr
09-14-2011, 08:37 PM
Here is an alternative using preg_replace_callback() (http://php.net/preg_replace_callback), just becase :) :

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

kbluhm
09-14-2011, 10:29 PM
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:


$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:


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

Inigoesdr
09-15-2011, 01:15 AM
Pffft. Doesn't randomize each time. Weakkk. :D

kbluhm
09-15-2011, 01:31 AM
D'oh!

BluePanther
09-15-2011, 06:43 PM
Lol :P. I like your method Inigoesdr. Forgot about the callback!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum