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;
}
random preg replace in an array?
Moderator: General Moderators
- dull1554
- Forum Regular
- Posts: 680
- Joined: Sat Nov 22, 2003 11:26 am
- Location: 42:21:35.359N, 76:02:20.688W
hmmmmmmm, thats tougher, you should be able to use str_replace
ex.
that should do the trick....try it out and if it does not work let me know
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