I will try to understand your algorithm to make sure the randomness is good and there are no strange things in there that would cause bias in the generated spin. Then I rebuild my spinner class on top of yours. Maybe if it is not too much to ask, you care to describe the algorithm for me so I understand it faster.
Sorry I just realized you asked a question here. Below is what I'm doing, commented. (PS, I didn't use code tags or PHP tags because I needed to highlight a few things).
function spin($pass){
$mytext = $pass;
while(inStr("}",$mytext)){
// keep spinning until no more } is found
$rbracket = strpos($mytext,"}",0);
// find the position of the very first closing bracket
$tString = substr($mytext,0,$rbracket);
// sets $tString as the entire string of text leading up to the first closing bracket
$tStringToken = explode("{",$tString);
// the short string before the first closing bracket may have 1, 2, 3, etc.
// (in your case sometimes up to 8) beginning brackets ... explode the string
// into an array that consists of all the beginning bracket instances
$tStringCount = count($tStringToken) - 1;
// this counts the array, which basically tells me exactly how many closing brackets
// there are in the initial string. I do this because I want the very last beginning
// bracket available in the initial string. I will only spin after the last beginning
// bracket because that is the one implied by the very first closing bracket
$tString = $tStringToken[$tStringCount];
// this returns the exact text that I want to spin on this while loop pass.
// for instance, in the following snippet ...
/*
I {love to spin|{like|enjoy} spinning}.
You could get 3 different sentences ...
I love to spin.
I like spinning.
I enjoy spinning.
I find the first closing bracket which is here ...
I {love to spin|{like|enjoy
} spinning}.
Then I find the last beginning bracket, which is here ...
I {love to spin|
{like|enjoy} spinning}.
Then (in this loop only) I have segregated this data and I will only (on this loop) spin the following.
I {love to spin|{
like|enjoy} spinning}.
*/
$tStringToken = explode("|",$tString);
// as in the above example I have segregated out like|enjoy. Now I turn into an array exploded with a pipe.
$tStringCount = count($tStringToken) - 1;
// as in above example, this code tells me my array has 2 potential combinations.
$i = rand(0,$tStringCount);
// as in above example, I make a random number which in this case will either be 0 or 1
$replace = $tStringToken[$i];
// as in above example, pulling 0 or 1 out of $tStringToken's array will produce like or enjoy randomly
$tString = "{".$tString."}";
// I rebuild the section I extracted so that I can do a search/replace reconstructing the original spintax variable.
// adding the brackets to it will allow me to fully remove the command to spin the section and replace it with the
// final version word which was selected randomly.
$mytext = str_replaceFirst($tString,$replace,$mytext);
// I replace the original extracted spintax snippet I just manipulated with the $replace var which is either like or enjoy.
}
return $mytext;
}
-----
Assuming on the first loop through and given a random of 0 or 1 ... it picks 0 it
will end up looking like this ...
I {love to spin|
like spinning}.
And if it were a 1 it will look like this ...
I {love to spin|
enjoy spinning}.
Then on the next pass through the first ending bracket it will find is at the end of spinning. Then it will find the first closing bracket immediately before the first ending bracket (the one in front of love). Then it will spin the love to spin, or enjoy spinning, with 0 or 1 randomly selected.
If you do it this way you could be nested into infinity and its fairly simple and quick to cycle through the spintax.
The main downside of the code is that it will loop into infinity if there are more } than {. I put a time limit of the execution of this script to prevent that from happening.