- preg_replace_callback: documentation ( source)
- array_rand: documentation ( source)
- explode: documentation ( source)
<?php
function spin($string)
{
return preg_replace_callback('/{(?<list>[^{}]+)}/', function ($match) {
$list = explode('|', $match['list']);
return $list[array_rand($list)];
}, $string);
}
$sentence = '{Please|Just} make this {cool|awesome|random} test sentence {rotate {quickly|fast} and random|spin and be random}';
echo spin($sentence);