Zend - The PHP Company




Menus & Navigation

Add Code


Logical Parser  

Type: code fragment
Added by: andrebl
Entered: 29/11/2002
Last modified: 02/11/2001
Rating: - (fewer than 3 votes)
Views: 4787
This code allows some conditioal testing for file based menus. I used it to write a wizzard that enable/disables menu entrys by selecting specific options. This code can evaluate statements like SESSION_VAR= as true ore false. In that way you can use a text file that contains a menu entry and an expression for the wizazrd steps.


<?php
// -----------------------------------------------------------------------------
// function : wzProcess                                                                                                                                    
// parameter: $str = string that represents an expression
//                                       ?expression
//                                        expression = token operator expression
//                                        token = 0|1|variable
//                                        operator = (),=,>,<,&,| 
//                                      
//                        example:
//                                        ?(ge_system=Fire280)&(ge_os=2.8)
//                                        evaluates true if the Variable ge_system is Fire280
//                                        and ge_os is 2.8. 
//
// return   : true or false
// purpose  : Evaluate a given expression to true ore false 
// -----------------------------------------------------------------------------
function wzProcess($string) {
                 static 
$str;
                 
$operators "?()|&=<>~";
                 
$token    "";
                 
$result   "";
                 
$operator "";
                 
                 
// set the loopcount to 0 and store the shortend string to the $str
                 // value. Cause $str is static, we will deal with the shortend string
                 // on return.
                 
$i 0;
                 
$str $string;
                 
// loop until we find an operator
                 
for (; $i<strlen($str);$i++) {
                          if (
strchr($operators,$str[$i])) {
                                 
$operator $str[$i];
                                break;
                         } else {
                         
$token .= $str[$i];
                         }
                 }
                 
                 
// this will replace the token if we have a ge_ session variable 
                 
$token wzIsVariable($token);
                 
                 
// process the operator that was found. 
                 
switch ($operator) {
                                case 
'?' :
                                         
$token wzProcess(substr($str,$i+1));
                                         break;
                                 case 
'(' :
                                         
$result wzProcess(substr($str,$i+1));
                                         
$str $result.$str;
                                         
$token wzProcess($str);
                                         break;
                                case 
')' :
                                         
$str substr($str,$i+1);
                                         return 
$token;
                                         break;                                         
                                case 
'|' :
                                         
$result wzProcess(substr($str,$i+1));
                                         
$token  wzOR($result,$token);
                                         break;
                                case 
'&' :
                                         
$result wzProcess(substr($str,$i+1));
                                         
$token  wzAND($result,$token);
                                         break;
                                case 
'=' :
                                         
$result wzProcess(substr($str,$i+1));
                                         
$token  wzEQUAL($result,$token);
                                         break;
                                case 
'~' :
                                         
$result wzProcess(substr($str,$i+1));
                                         
$token  wzISIN($result,$token);
                                         break;
                                case 
'<' :
                                         
$result wzProcess(substr($str,$i+1));
                                         
$token  wzLESS($token$result);
                                         break;
                                case 
'>' :
                                         
$result wzProcess(substr($str,$i+1));
                                         
$token  wzBIGGER($token$result);
                                         break;
                 }
                 
// return the token for further processing
                 
return $token;
}

// -----------------------------------------------------------------------------
// function : wzOR                                                                                                                                    
// parameter: $var1 -> The first value
//                         $var2 -> Second value
// return   : true or false
// purpose  : This function can used to execute an OR between to values 
//                         result is true if one of the variables is true. 
// -----------------------------------------------------------------------------
function wzOR($val1,$val2) {
                 return (bool)(
$val1 $val2);                 
}

// -----------------------------------------------------------------------------
// function : wzAND                                                                                                                                
// parameter: $var1 -> The first value
//                         $var2 -> Second value
// return   : true or false
// purpose  : This function can used to execute an AND between to values 
//                         result is true if both variables are true. 
// -----------------------------------------------------------------------------
function wzAND($val1,$val2) {
                 return (bool)(
$val1 $val2);
}

// -----------------------------------------------------------------------------
// function : wzISIN                                                                                                                                    
// parameter: $value   -> the value that is searched within the
//                         $pattern -> pattern 
// return   : true or false
// purpose  : This function can used to match a pattern between to values 
//                         If the pattern is found it will return true. 
//                        NOTICE: if you match against seperated strings like "1 2 3"
//                                        an <space> should be added to the $value string.  
// -----------------------------------------------------------------------------
function wzISIN($value,$pattern) {
                 if (
preg_match("/".$value."/",$pattern." ")) return 1;
                 return 
0;
}

// -----------------------------------------------------------------------------
// function : wzBIGGER                                                                                                                                    
// parameter: $var1 -> The first value
//                         $var2 -> Second value
// return   : true or false
// purpose  : var1 is bigger then var 2 (integer compare) 
// -----------------------------------------------------------------------------
function wzBIGGER($val1,$val2) {
                 if (
$val1 $val2) {
                        return 
1;
                 }
                 return 
0;
}

// -----------------------------------------------------------------------------
// function : wzLESS                                                                                                                                    
// parameter: $var1 -> The first value
//                         $var2 -> Second value
// return   : true or false
// purpose  : var1 is less then var2 (integer compare) 
// -----------------------------------------------------------------------------
function wzLESS($val1,$val2) {
                 if (
$val1 $val2) {
                        return 
1;
                 }
                 return 
0;
}

// -----------------------------------------------------------------------------
// function : wzEQUAL                                                                                                                                    
// parameter: $var1 -> The first value
//                         $var2 -> Second value
// return   : true or false
// purpose  : var1 is equal to var2 (integer compare/ may also work on strings) 
// -----------------------------------------------------------------------------
function wzEQUAL($val1,$val2){
                 if (
$val1 == $val2) return 1;
                 return 
0;

}

// -----------------------------------------------------------------------------
// function : wzIsVariable                                                                                                                                
// parameter: $var -> The value to check
//
// return   : alway a value 
// purpose  : if the given string is a $_SESSION variable, this function will 
//                         return the content of this variable. This function can be enhanced
//                        for other cases. 
// -----------------------------------------------------------------------------
function wzIsVariable($var) {
                 if (
preg_match("/ge_/",$var)) {
                         if (isset(
$_SESSION[$var])) {
                             return 
$_SESSION[$var];
                        } else {
                            return 
0;
                        }
                 }
                 return 
$var;
}
?>


Usage Example




Rate This Script





Search



This Category All Categories