root/include/functions_search.php

Revision 331, 4.2 kB (checked in by Nafania, 1 year ago)

фича - похожие торренты

Line 
1 <?php
2
3 function searchfield($entry)</span>
4 <span class="code-keyword">{
5     static $drop_char_match =   array('^', '$', '&', '(', ')', '<', '>', '`', '"', '|', ',', '@', '_', '?', '%', '-', '~', '+', '.', '[', ']', '{', '}', ':', '\\', '/', '=', '#', '\'', ';', '!', '+', '-', '|');
6     static $drop_char_replace = array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ''' ', ' ', ' ', ' ', ''' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' , ' ', ' ', ' ', ' '' ', ' ', ' ', ' ', ' ');
7
8     $entry = strip_tags(utf_strtolower($entry));
9
10     $entry = str_replace(' +', ' and ', $entry);
11     $entry = str_replace(' -', ' not ', $entry);
12     $entry = str_replace(' |', ' or ', $entry);
13
14     //
15     // Filter out strange characters like ^, $, &, change "it's" to "its"
16     //
17     for($i = 0; $i < sizeof($drop_char_match); $i++)
18     {
19         $entry str_replace($drop_char_match[$i], $drop_char_replace[$i], $entry);
20     }
21
22     return $entry;
23 }
24
25 function split_words($entry, $mode = 'post')
26 {
27         return explode(' ', trim(preg_replace('#\s+#', ' ', $entry)));
28 }
29
30 function search_text_in_db ( $searchstr, $base_sql, $where_search, $add_where = array(), $strict = false ) {
31     global $db, $config;
32
33     //$stopword_array = @file($root_path . 'languages/lang_' . $config['default_lang'] . '/search_stopwords.txt');
34     //$synonym_array = @file($root_path . 'languages/lang_' . $config['default_lang'] . '/search_synonyms.txt');
35
36     $match_types = array('or', 'not', 'and');
37
38     $add_where = ( sizeof($add_where) ? ' AND ' . implode(' AND ', $add_where) : '' );
39
40     $cleansearchstr = searchfield($searchstr);
41     $lower_searchstr = utf_strtolower($searchstr);
42
43     if ( $strict ) {
44         $split_search = array($lower_searchstr);
45     }
46     else {
47
48         $split_search = split_words($cleansearchstr);
49
50         if ( $lower_searchstr <> $searchstr ) {
51             $search_full_string = true;
52             foreach ( $match_types  AS $_null => $match_type ) {
53                 if ( strpos($lower_searchstr, $match_type) !== false ) {
54                     $search_full_string = false;
55                 }
56             }
57             if ( $search_full_string ) {
58                 $split_search[] = $lower_searchstr;
59             }
60         }
61     }
62
63     $word_count = 0;
64     $current_match_type = 'and';
65
66     $word_match = array();
67     $result_list = array();
68
69     for($i = 0; $i < sizeof($split_search); $i++)
70     {
71         if ( utf_strlen(str_replace(array('*', '%'), '', trim($split_search[$i]))) < $config['search_min_chars'] && !in_array($split_search[$i], $match_types) )
72         {
73             $split_search[$i] = '';
74             continue;
75          }
76
77         switch ( $split_search[$i] )
78          {
79             case 'and':
80                 $current_match_type = 'and';
81             break;
82
83             case 'or':
84                 $current_match_type = 'or';
85             break;
86
87             case 'not':
88                 $current_match_type = 'not';
89             break;
90
91             default:
92                 if ( !empty($search_terms) ) {
93                     $current_match_type = 'and';
94                 }
95
96                 if ( $strict ) {
97                     $search = $where_search . ' = \'' . $db->sql_escape($split_search[$i]) . '\'' . $add_where;
98                 }
99                 else {
100                     $match_word = str_replace('*', '%', $split_search[$i]);
101                     $search = $where_search . ' LIKE \'%' . $db->sql_escape($match_word) . '%\'' . $add_where;
102                     //$search = $where_search . ' REGEXP \'[[:<:]]' . $db->sql_escape($match_word) . '[[:>:]]\'' . $add_where;
103                 }
104
105                 $sql = $base_sql . ' WHERE ' . $search;
106                 $result = $db->sql_query($sql);
107
108                 $row = array();
109
110                 while( $temp_row = $db->sql_fetchrow($result) ) {
111                     $row[$temp_row['id']] = 1;
112
113                     if ( !$word_count )
114                     {
115                         $result_list[$temp_row['id']] = 1;
116                     }
117                     else if ( $current_match_type == 'or' )
118                     {
119                         $result_list[$temp_row['id']] = 1;
120                     }
121                     else if ( $current_match_type == 'not' )
122                     {
123                         $result_list[$temp_row['id']] = 0;
124                     }
125                 }
126
127                 if ( $current_match_type == 'and' && $word_count ) {
128                     @reset($result_list);
129
130                     foreach ( $result_list AS $id => $match_count ) {
131                         if ( !isset($row[$id]) || !$row[$id] ) {
132                             //$result_list[$id] = 0;
133                             @$result_list[$id] -= 1;
134                         }
135                         else {
136                             @$result_list[$id] += 1;
137                         }
138                     }
139                 }
140
141                 $word_count++;
142
143                 $db->sql_freeresult($result);
144             }
145         }
146         @reset($result_list);
147
148         $search_ids = array();
149
150         foreach ( $result_list AS $id => $matches ) {
151             if ( $matches > 0 ) {
152             //if ( $matches ) {
153               $search_ids[] = $id;
154             }
155         }
156
157     unset($result_list);
158
159     return $search_ids;
160 }
161
162
163 ?>
Note: See TracBrowser for help on using the browser.