<?
// Class dealing with alternative strings:
// version 1.6 - now with regexps
// String Syntax: some text (h|e|e|l|l|l|l|o| |,|world) some text
class alternate {
var $s;
var $pattern;
function ms () {
//seeding
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
srand(make_seed());
}
function bracket ($str) {
//returns bracket code
if (!strcmp($str,"(")) {return 1;}
elseif (!strcmp($str,")")) {return -1;} else {return 0;}
}
function alternate ($s="") {
$this->ms();
$this->s=$s;
$this->pattern="/\(([^\(]*?)\)/m";
while (preg_match($this->pattern,$this->s)!=0)
$this->s=preg_replace_callback ($this->pattern,
create_function (
'$matches',
'$store=explode("|",$matches[1]);
$store=explode("|",$matches[1]);
$rand_key=$store[array_rand($store,1)];
return $rand_key;'),
$this->s);
}
}
$alter=new alternate ("Running script\n((ha\nil|cr\ny|hello), (world|gold|silver)|how do you (do|did|man)?|anybody here?)");
echo $alter->s;
?>
|