<?
// by Al Amzin [hide@address.com]
// Class dealing with alternative strings:
// String Syntax: (h|e|e|l|l|l|l|o| |,|world)
// ONE bracket pair is a MUST!
// LEGAL: "(boy|girl)"
// ILLEGAL: " boy|girl"
class alternate {
var $s;
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;
//Main code
while (1) {
$depth=0;
$depthpos=0;
$maxdepth=0;
for ($i=0; $i<=strlen($this->s);$i++) {
$chr=substr($this->s,$i,1);
$depth+=$this->bracket($chr);
if ($depth>$maxdepth) {$maxdepth=$depth; $depthpos=$i;}
}
$i=$depthpos;
$small="";
while (1) {
$chr=substr($this->s,$i,1);
if ($this->bracket($chr)!=-1) {$small=$small.$chr;} else {break;}
$i++;
}
$len=$i-$depthpos;
$small=str_replace ("(","",$small);
$sm=explode("|",$small);
$sz=count($sm);
$alt=rand (0, $sz-1);
$this->s=substr($this->s,0,$depthpos).$sm[$alt].substr($this->s,$i+1);
if ($maxdepth==1) break;
}
}
}
$alter=new alternate ("(hello, world|how do you do?|anybody here?)");
echo $alter->s;
?>