Page 1 of 1

random preg replace in an array?

Posted: Fri Jan 23, 2004 9:24 pm
by Akemi
I'm really new at this, but is there a way to randomly pick a line from the array then preg replace it?


For the below string i just want some of the 5 letters from the array to randomly stutter. I don't want all the c,d,e,s,t to preg replace. just randomly any of them.


thanks!

code>>>>>>


$string = "2. The Alphabet Game
Starting with challenge your child to find each letter of the alphabet on signs as you travel. Players call out the letter and the word it's in as they see each one -- the first one to reach wins. Vary the action by limiting the places to find letters to only license plates, road signs, or billboards.";



$tr_list = array(


"/c/" => "c-c-c.....",
"/d/" => "d-d-d....",
"/e/" => "e-e-e....",
"/s/" => "s-s-s....",
"/t/" => "t-t-t...."



);
$random = rand(0,sizeof($tr_list)-1);
//echo $random;
//echo sizeof($tr_list);
//list($a[0], $a[1], $a[2], $a[3], $a[4]) = $tr_list;
//echo $tr_list;
//echo $string;

foreach ($tr_list as $key => $value) {

$translateThis = preg_replace($key, $value, $string);
echo $string;
}

Posted: Fri Jan 23, 2004 9:31 pm
by dull1554
if you know which line you want to replace why dont you just reset the value of that line of the array

Posted: Fri Jan 23, 2004 9:45 pm
by Akemi
no that's the thing, i don't know what line. i just want some of the letters to be randomly replaced.

Posted: Fri Jan 23, 2004 9:58 pm
by dull1554
hmmmmmmm, thats tougher, you should be able to use str_replace

ex.

Code: Select all

$array = str_replace('a','q',$array);//('letter to be replaced','replace with','new $var name')
//doing it this way its still $array after you've replaced the chars, and you can repete this as many times as you want to untill you have replaced all the chars you need to replace
that should do the trick....try it out and if it does not work let me know