Hi, I am using this function which is used to spin text. Currently it looks to me like it is selected an element from the $tStringToken array at random. These two lines seem to be doing that: Expand|Select|Wrap|Line Numbers $i = rand(0,$tStringCount); $replace = $tStringToken[$i]; I would like to change this so that it goes sequentially through all of the available elements in the array and then when finished starts again with the first element of the array. But I am having trouble working it out. This is the function I am working on. Expand|Select|Wrap|Line Numbers function spin($pass){ $mytext = $pass; while(inStr("}",$mytext)){ $rbracket = strpos($mytext,"}",0); $tString = substr($mytext,0,$rbracket); $tStringToken = explode("{",$tString); $tStringCount = count($tStringToken) - 1; $tString = $tStringToken[$tStringCount]; $tStringToken = explode("|",$tString); $tStringCount = count($tStringToken) - 1; $i = rand(0,$tStringCount); $replace = $tStringToken[$i]; $tString = "{".$tString."}"; $mytext = str_replaceFirst($tString,$replace,$mytext); } return $mytext; } Any ideas on how I can do this would be very helpful Thanks.
I'm a little drunk, but how's this? function spin($text) { static $calls = []; return preg_replace_callback('~\{([^\}]+)\}~', function (array $words) use (&$calls) { $key = $words[0]; $words = explode('|', $words[1]); if (!isset($calls[$key])) { $calls[$key] = 0; } return $words[$calls[$key]++ % sizeof($words)]; }, $text); } PHP: