- preg_replace_callback: documentation ( source)
- array_rand: documentation ( source)
- explode: documentation ( source)
<?php
$sentence = '{Please|Just} make this {cool|awesome|random} test sentence {rotate {quickly|fast} and random|spin and be random}';
function sentence_spinning($str)
{
$new_str = preg_replace_callback('#\{([^{}]*)\}#im', function($matches)
{
$rand = array_rand($split = explode('|', $matches[1]));
return $split[$rand];
}, $str);
return ($new_str !== $str) ? sentence_spinning($new_str) : $str;
if ($new_str !== $str) $str = sentence_spinning($new_str);
return $str;
}
echo sentence_spinning($sentence);