Hi, Here is what I have: I have the above string. I need to select a random word from each of the { } Example result: Easiest way to do it? Thanks
$string = '{word1/word2/word3} {word4/word5/wor6/word7} {word8/word9}'; $words = array(); $sets = preg_split('~(\} )?[\{\}]~', $string, -1, PREG_SPLIT_NO_EMPTY); foreach ($sets AS $set) { $tmp_words = explode('/', $set); $words[] = $tmp_words[array_rand($tmp_words)]; } print_r($words); PHP:
Thanks nico, it works great. You saved me one more time One problem though. Between the } { it may be other characters. Ex: $string = '{word1/word2/word3} abc{word4/word5/wor6/word7}etc.{word8/word9}'; PHP: I only need to choose a random word between { }, the rest should remain the same. So, I will output word2 abcword6etc.word8 Code (markup): I do care about space chars, as I must output them too. Cheers mate
<?php $pat = '#\{(?:[^/]+/){%d}([^/}]+)(?:/[^}]+)?\} \{(?:[^/]+/){%d}([^/}]+)(?:/[^}]+)?\} \{(?:[^/]+/){%d}([^/}]+)(?:/[^}]+)?\}#'; $str = '{the cat/my dog/a super sexy cheetah} {drank/meowed loudly/went to the bathroom/chilled} {with me/by itself}'; $rpat = sprintf($pat, rand(0,2), rand(0,3), rand(0,1)); echo $rpat, '<br/>', preg_replace($rpat, "$1 $2 $3", $str) ?> PHP:
Joebert only one problem. I may have more then 3 pairs of {} Your code works too, but not for more than 3 pairs. Thanks
How many words/sections do you anticipate ? preg_replace can deal with a maximum of 99 back references.
$string = '{word1/word2/word3} abc{word4/word5/wor6/word7}etc.{word8/word9}'; $words = array(); if (preg_match_all('~\{([^\}]+)\}~', $string, $matches)) { foreach ($matches[1] AS $set) { $tmp_words = explode('/', $set); $words[] = $tmp_words[array_rand($tmp_words)]; } } print_r($words); PHP:
preg_replace_callback might be a good fit with this task. <?php $str = '{word1/word2/word3} abc{word4/word5/wor6/word7}etc.{word8/word9}'; $str = preg_replace_callback( '#\{[^}]+\}#', create_function( '$match', '$match = explode(\'/\', trim($match[0], \'{}\')); return $match[rand(0,sizeof($match)-1)];' ), $str ); echo $str; ?> PHP: