home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / itowhere.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  1.6 KB  |  54 lines

  1. Input to WHERE 
  2.  
  3. The b_parse function takes a database field and a query string and returns a WHERE clause for use in a database query. It supports "AND", "NOT" and "OR" keywords in the query string, but also supports quoting of "exact phrases" to match. 
  4.  
  5.  
  6. <?php 
  7.  
  8. function b_parse($str, $field)  
  9.    if($str) {  
  10.    $quoted = explode( "\\\"", $str); 
  11.              
  12.    for($i = 0; $i < count($quoted); $i++) { 
  13.       if($i == 0 && !$quoted[$i]) { 
  14.        //quote came at beginning of string 
  15.       $begin = True; 
  16.       $i++; 
  17.       } 
  18.       if($begin) { $words[] = $quoted[$i]; } 
  19.       else { 
  20.          $phrase = explode( " ", $quoted[$i]); 
  21.          for($n = 0; $n < count($phrase); $n++) { 
  22.             if($phrase[$n]) { $words[] = $phrase[$n]; } 
  23.          } 
  24.       } 
  25.       $begin = !$begin; 
  26.    }             
  27.              
  28.    for($i = 0; $i < count($words); $i++) { 
  29.       if($words[$i]) { 
  30.          if($words[$i] ==  "and" || $words[$i] ==  "or" || $words[$i] ==  "not") { 
  31.             if($words[$i] ==  "not") { 
  32.                $i++; 
  33.                if($sql_out) { $sql_out .=  " AND "; } 
  34.                $sql_out .= $field .  " NOT LIKE '%" . $words[$i] .  "%'"; 
  35.              } 
  36.              else if($i > 0) { 
  37.                $sql_out .=  " " . strtoupper($words[$i]) .  " "; 
  38.                $boolean = True; 
  39.              } 
  40.           } 
  41.           else { 
  42.             if($sql_out && !$boolean) { $sql_out .=  " OR "; } 
  43.             $sql_out .= $field .  " LIKE '%" . $words[$i] .  "%'"; 
  44.             $boolean = False; 
  45.           } 
  46.       } 
  47.     } 
  48.     } 
  49.     return $sql_out; 
  50.  
  51. ?>
  52.