$match) { $choices = explode('|', $matches[1][$key]); if($calculate) { $permutations *= count($choices); } else { $find[] = $match; $replace[] = $choices[mt_rand(0, count($choices) - 1)]; } } if(!$calculate) { # Ensure multiple instances of the same spinning combinations will spin differently $string = str_replace_first($find, $replace, $string); } } return $$return; } # Similar to str_replace, but only replaces the first instance of the needle function str_replace_first($find, $replace, $string) { # Ensure we are dealing with arrays if(!is_array($find)) { $find = array($find); } if(!is_array($replace)) { $replace = array($replace); } foreach($find as $key => $value) { if(($pos = strpos($string, $value)) !== false) { # If we have no replacement make it empty if(!isset($replace[$key])) { $replace[$key] = ''; } $string = mb_substr($string, 0, $pos).$replace[$key].mb_substr($string, $pos + mb_strlen($value)); } } return $string; } // end of function $string = '{{The|A}} {{quick|speedy|fast}} {{brown|black|red}} {{fox|wolf}} {{jumped|bounded|hopped|skipped}} over the {{lazy|tired}} {{dog|hound}}'; echo '

'.spin($string, false, true).' permutations...

'; for($i = 1; $i <= 10; $i++) { echo spin($string, false).'
'; } echo '
'; ?>