21,886
社区成员
发帖
与我相关
我的任务
分享
function simple($logic){ // convert logic to simple
//echo $logic."\n";
$left_count = substr_count($logic,'(');
$right_count= substr_count($logic,')');
if($left_count!=$right_count){
return false;
}
if($left_count==0){
return $logic;
}
$exist_nest=preg_match('/\([^\)]*?\(/',$logic);
if($left_count==1){
$temp=preg_replace('/\(.*?\)/','',$logic);
if(empty($temp)){
$str=trim(trim($logic,')'),'(');
return $str;
}
$and_count=substr_count($temp,'&');
$or_count=substr_count($temp,'|');
if( $and_count==0 || $and_count==1 && $or_count==0 ){ // x&(...) or (...)&x or y|x|(..)|z
// only 1 pair ( and ) , remove them
if(strpos($logic,'&(')!==false){
list($left,$right)=explode('&(',$logic);
$right=trim($right,')');
$right_elements=explode('|',$right);
$str='';
foreach($right_elements as $r){
$str.=$left.'&'.$r.'|';
}
$str=rtrim($str,'|');
return $str;
}elseif(strpos($logic,')&')!==false ){
list($left,$right)=explode(')&',$logic);
$left=trim($left,'(');
$left_elements=explode('|',$left);
$str='';
foreach($left_elements as $r){
$str.=$r.'&'.$right.'|';
}
$str=rtrim($str,'|');
return $str;
}else{
$str=str_replace(array('(',')'),'',$logic);
return $str;
}
}
}
if($left_count>=2){ // ()&() or ()|()|x|y|()....
if(!$exist_nest){
$temp=preg_replace('/\(.*?\)/','',$logic);
$and_count=substr_count($temp,'&');
$or_count=substr_count($temp,'|');
//if( $and_count==0 || $and_count==1 && $or_count==0 ){
if($temp=='&' || $and_count==0 ){
if($and_count==0){ // ()|()|()|x|()|y...
$str=str_replace(array('(',')'),'',$logic);
return $str;
}
if($temp=='&'){
list($left,$right)=explode(')&(',$logic);
$left=trim($left,'(');
$right=trim($right,')');
$left_elements=explode('|',$left);
$right_elements=explode('|',$right);
$str='';
foreach($left_elements as $l){
foreach($right_elements as $r){
$str.=$l.'&'.$r.'|';
}
}
$str=trim($str,'|');
return $str;
}
}
}
}
$strs=preg_split('/(\)|\(|&|\|)/',$logic,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
$result=array();
$stack=array();
$level=0;
$mark=0;
//find a matched ( and )
$strs=array_reverse($strs);
while($str=array_pop($strs)){
//echo 'RESULT:'.implode('',$result)."\t\t".implode('',$stack)."\n";
if($str=='('){
$level++;
if($level>1 || $mark==1) array_push($stack,$str);
continue;
}
if($str==')'){
$level--;
if($level>0 || $mark==1) array_push($stack,$str);
if($mark==1 && $level==0){
$mark=0;
//$level--;
}
if($level==0){ // end of ()
$temp=implode('',$stack);
$stack=array();
$mid=simple($temp);
array_push($result,'('.$mid.')');
}
continue;
}
if($level==0){
if($str=='&' && !$exist_nest ){
$left=array_pop($result); //echo '['.$left.']';
array_push($stack,$left);
array_push($stack,'&');
//$level++;
$mark=1;
continue;
}
if($str!='|' && $str!='&'){ // is a number
if($mark==1){
$mark=0;
array_push($stack,$str);
array_push($result,'('.implode('',$stack).')');
$stack=array();
continue;
//array_push($stack,')');
//$level--;
}
array_push($result,$str);
}else{
array_push($result,$str);
}
}else{
array_push($stack,$str);
}
}
if($level!=0) {
return false;
}
return simple(implode('',$result));
}