Random Sentence Spinning

Usage: Just put word inside brackets separated by "|" (default) delimiter and echo it.


function randomize_sentence($sentence, $start = "{",$stop = "}", $delimiter = "|"){
srand((double) microtime() * 1234567);
$random = $sentence;
$regexp = "/\\$start(.*?)\\$stop/is";

preg_match_all($regexp, $sentence, $matches);
unset($matches[0]);
foreach($matches as $words){
    foreach($words as $word){
		$final = explode($delimiter, $word);
		$random = str_replace($word, $final[mt_rand(0, count($final)-1)], $random);
    }
}

$random = preg_replace("/[\{|\}]/", "", $random);
return $random;
}

for($i = 0; $i < 5; $i++){
    $new = randomize_sentence("{I did|I didn't} get a job at First Beat Media because {I was able|I wasn't able} to make {this thing.|recursive brackets for this.}");
    echo $new."";
}
©2009-2011 Webarto • web design & development • Tuzla // Sarajevo // Beograd