Random Sentence Spinning Function
Interview task @ First Beat Media for Junior/Senior PHP developer.
Old:
http://webarto.com/11/random-sentence-spinning
function get_random($matches)
{
$rand = array_rand($split = explode("|", $matches[1]));
return $split[$rand];
}
function show_randomized($str)
{
$new_str = preg_replace_callback('/\{([^{}]*)\}/im', "get_random", $str);
if ($new_str !== $str) $str = show_randomized($new_str);
return $str;
}
Usage example:
$rand_sentence = "{Please|Just} make this {cool|awesome|random} test sentence {rotate {quickly|fast} and random|spin and be random}";
for ($i=0; $i < 10; $i++)
{
echo show_randomized($rand_sentence).'';
}