From dc6ed70273afd85fd0191d81965ea5969ec291a5 Mon Sep 17 00:00:00 2001 From: jwangen Date: Wed, 23 Oct 2002 07:22:44 +0000 Subject: Modified search.php, still not complete --- search.php | 151 +++++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 121 insertions(+), 30 deletions(-) (limited to 'search.php') diff --git a/search.php b/search.php index 24ade80..d31d461 100644 --- a/search.php +++ b/search.php @@ -2,31 +2,128 @@ include('./functions/ical_parser.php'); -$search_string = 'final'; -$search_arr = explode(' ', $search_string); +// takes a boolean search and a string. Returns an array with +// [0] = True/False and [1] = formatted search string +function search_boolean($needle,$haystack) { + // init arrays + $and_arr = array(); + $or_arr = array(); + $not_arr = array(); + $or_str_arr = array(); -function array_in($string,$arr) { - foreach($arr as $s) { - if (eregi($s,$string) == false) { - return false; + // compare lowercase versions of the strings + $haystack = strtolower($haystack); + $needle = strtolower($needle); + + // clean up search string + $needle = str_replace(' and ', ' ', $needle); + $needle = ereg_replace('[[:space:]]+',' ', $needle); + + // start out with an AND array of all the items + $and_arr = explode(' ', $needle); + $count = count($and_arr); + $j = 0; + + // build an OR array from the items in AND + for($i=0;$i<$count;$i++) { + if ($i != 0 && $and_arr[$i] == 'or') { + while ($and_arr[$i] == 'or') { + $or_arr[$j][] = $and_arr[$i-1]; + unset($and_arr[$i], $and_arr[$i-1]); + $i += 2; + } + if (isset($and_arr[$i-1])) { + $or_arr[$j][] = $and_arr[$i-1]; + unset($and_arr[$i-1]); + } + $or_str_arr[$j] = implode(' OR ', $or_arr[$j]); + $j++; + } + } + + // build a NOT array from the items in AND + foreach($and_arr as $key => $val) { + if (substr($val,0,1) == '-') { + $not_arr[] = substr($val,1); + unset($and_arr[$key]); + } elseif(substr($val,0,1) == '+') { + $and_arr[] = substr($val,1); + unset($and_arr[$key]); + } + } + + // prepare our formatted search string + if (count($and_arr) > 1) { + $final_str_arr[] = implode(' AND ', $and_arr); + } elseif (isset($and_arr[0]) && $and_arr[0] != '') { + $final_str_arr[] = $and_arr[0]; + } + + if (count($or_str_arr) > 1) { + $final_str_arr[] = implode(' AND ', $or_str_arr); + } elseif (isset($or_str_arr[0]) && $or_str_arr[0] != '') { + $final_str_arr[] = $or_str_arr[0]; + } + + if (count($not_arr) > 1) { + $final_str_arr[] = '-'.implode(' AND -', $not_arr); + } elseif (isset($not_arr[0]) && $not_arr[0] != '') { + $final_str_arr[] = '-'.$not_arr[0]; + } + + if (count($final_str_arr) > 1) { + $formatted_search = ''.implode(' AND ', $final_str_arr).''; + } else { + $formatted_search = ''.$final_str_arr[0].''; + } + + // check against the NOT + foreach($not_arr as $s) { + if (ereg($s, $haystack) == true) { + return array(false,$formatted_search); } } - return true; + + // check against the AND + foreach($and_arr as $s) { + if (ereg($s,$haystack) == false) { + return array(false,$formatted_search); + } + } + + // check against the OR + foreach($or_arr as $or) { + $is_false = true; + foreach($or as $s) { + if (ereg($s,$haystack) == true) { + $is_false = false; + break; + } + } + if ($is_false) return array(false,$formatted_search); + } + + // if we haven't returned false, then we return true + return array(true,$formatted_search); } -function search($find_arr) { - global $master_array; - if (isset($master_array) && is_array($master_array)) { - foreach($master_array as $date_key => $date) { - if (is_array($date)) { - foreach($date as $time_key => $time) { - if (is_array($time)) { - foreach ($time as $event_key => $event) { - if (is_array($event)) { - if (array_in($event['description'],$find_arr) || array_in($event['event_text'],$find_arr)) { - $tmp_arr = $event; - $tmp_arr['date'] = $date_key; - $retarr[] = $tmp_arr; - } + +$search_string = 'final japan'; + +if (isset($master_array) && is_array($master_array)) { + foreach($master_array as $date_key_tmp => $date_tmp) { + if (is_array($date_tmp)) { + foreach($date_tmp as $time_tmp) { + if (is_array($time_tmp)) { + foreach ($time_tmp as $event_tmp) { + if (is_array($event_tmp)) { + $results1 = search_boolean($search_string,$event_tmp['event_text']); + $formatted_search = $results1[1]; + if (!$results1[0]) { + $results2 = search_boolean($search_string,$event_tmp['description']); + } + if ($results1[0] || $results2[0]) { + $event_tmp['date'] = $date_key_tmp; + $the_arr[] = $event_tmp; } } } @@ -34,11 +131,8 @@ function search($find_arr) { } } } - return $retarr; } - -$the_arr = search($search_arr); - + ?> \"right\""; ?> - + \"right\""; ?> - - - @@ -69,7 +160,7 @@ $the_arr = search($search_arr);
- + -- cgit v1.2.3
"; ?> "; ?>