//This generates random text from a Markov process. A state is considered to be a single word, //separated by one or more spaces //A word can include a puntuation mark. //The list of words is considered as a cycle so that the last word transtitions to the first function RandomizeTextOrder1($strText,$maxLength=0) { $arrWords=preg_split("/[\s\t\r\n]+/", $strText); if(count($arrWords)<3): //Just return original text if it is really short return ""; endif; $arrTransitions=array(); //This holds the transitions that are found //Find the transtitions $w1=$arrWords[0]; $wordNum=1; while(array_key_exists($wordNum,$arrWords)) : $state=$w1; if(!array_key_exists($state,$arrTransitions)) : $arrTransitions[$state]=array(); endif; $newState=$arrWords[$wordNum]; $arrTransitions[$state][]=$newState; $w1=$arrWords[$wordNum]; $wordNum=$wordNum+1; endwhile; //Add on the transtition from the last word to the first word $state=$w1; $arrTransitions[$state]=array($arrWords[0]); if($maxLength==0) : $maxLength=count($arrWords); //Use the number of words in original text as default number in random text endif; srand(time()); $CurrentState=$arrWords[0]; //Start the same way the original text starts. $CurrentLength=0; $RandomText=""; while($CurrentLength<$maxLength) : $RandomText.=" ".$CurrentState; $NextStateIndex=rand(0,count($arrTransitions[$CurrentState])-1); $CurrentState=$arrTransitions[$CurrentState][$NextStateIndex]; //Change states $CurrentLength=$CurrentLength+1; endwhile; return $RandomText; }