<?
/**
* text spinner by gdinko
*/
session_start();
$text = "{Please|Just} make this {cool|awesome|random} test sentence
{rotate {quickly|fast} and random|spin and be random}";
$pattern = '/\{([^\{\}]+)\}/';
//fix the text
$match = '';
do{
$text = preg_replace_callback($pattern, 'spin', $text);
preg_match($pattern, $text, $match);
} while (!empty($match));
//echo fixed text
echo $text;
//the magic
function spin($_spin)
{
$ret = '';
if(isset($_spin[1])){
$words_array = preg_split('/\|/', $_spin[1]);
if(($count = count($words_array)) > 1){
//used like registry
//if some pattern is same in the text make sure to get different words
//using this like registry
if(isset($_SESSION['registry'][$_spin[1]]['last_word'])){
while (($word = $words_array[rand(0,$count-1)]) == $_SESSION['registry'][$_spin[1]]['last_word']);
} else {
$word = $words_array[rand(0,$count-1)];
}
$_SESSION['registry'][$_spin[1]]['last_word'] = $word;
$ret = $word;
} else {
$ret = $words_array[0];
}
}
return $ret;
}