Match word in search Match word in search
 

News:

CPG Release 1.6.26
Correct PHP8.2 issues with user and language managers.
Additional fixes for PHP 8.2
Correct PHP8 error with SMF 2.0 bridge.
Correct IPTC supplimental category parsing.
Download and info HERE

Main Menu

Match word in search

Started by krakelis, December 31, 2011, 02:15:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

krakelis

After upgrading from CPG 1.4.19 to 1.5.16 I did hope to find the option 'match word' in search.php wich is not the case. I did search and read every thread on this forum about this subject without finding the solution. It seems to be technical impossible for those who can not write PHP themselve. For my extended image library it still would be great if searching for 'male' would not give results with 'female', at least as an extra option. So I really would like to extend search.php with the option 'match word'.

All my hope is on the new implemented regex option but I read somewhere in this forum that Regex for Mysql has no code for 'match word'. For me this is hard to believe and after lot of searching I do indeed find something on the web. [[:<:]] matches beginning of word, [[:>:]] matches ending of word. When I try this code in search.php within the regex option the searchresults seem good to me. So it seems possible to match word with Regex in Mysql.

My next step was to make an extra option in search.php to comfort users because typing [[:<:]]searchword[[:>:]] is no fun at all. In this added regex option the code for begin and end of a word is added later in the code in include/search.inc.php. My I-can-fix-everything-trick is to repeat the regex code in include/search.inc.php and put the wordbegin/wordend code around the search string. I do have no knowledge at all of PHP and other coding/scripting so I would appreciate very much if anyone could check my code. It seems to work but does it really make sense this way? Is the syntax correct, do I oversee important things,etc? Please let me know.

You can find the result here:http://basdekker.eu/beeldbank/search.php

In search.php:

old code

</td>
<td align="right">
<select name="type" class="listbox">
<option value="AND" selected="selected">{$lang_search_php['all_words']}</option>
<option value="OR">{$lang_search_php['any_words']}</option>
<option value="regex">{$lang_search_php['regex']}</option></select>
</td>


new code

</td>
<td align="right">
<select name="type" class="listbox">
<option value="AND" selected="selected">{$lang_search_php['all_words']}</option>
<option value="OR">{$lang_search_php['any_words']}</option>
<option value="word">{$lang_search_php['word']}</option>
<option value="regex">{$lang_search_php['regex']}</option></select>
</td>


In lang/dutch.php:

old code

$lang_search_php['days'] = 'dagen';
$lang_search_php['all_words'] = 'zoek ALLE woorden (AND)';


new code


$lang_search_php['days'] = 'dagen';
$lang_search_php['word'] = 'zoek exact woord';
$lang_search_php['all_words'] = 'zoek ALLE woorden (AND)';


In include/search.inc.php:

old code

if ($search_string && isset($search_params['params'])) {
        $sections = array();
        $albcat_terms = array(); // For Album & Category Title Search: populated as needed
        if ($search_params['params']['type'] == 'regex') {
                $fields = array();
                $search_string = preg_replace('/[^\w\+\*\?\{\,\}\|\(\)\\\^\$\[\]\:\<\>\-\.]/','',$search_string);
                $search_string = addslashes($search_string);
                if ($superCage->get->keyExists('album_title') || $superCage->get->keyExists('category_title')) $albcat_terms[] = " REGEXP '$search_string'";               
                foreach ($search_params['params'] as $param => $value) {
                        if (in_array($param, $allowed)) $fields[] = "$param REGEXP '$search_string'";
                }
                $sql .= count($fields) ? ('((' . implode(' OR ', $fields) . '))') : '';
         } else {
                $search_string = strtr($search_string, array('_' => '\_', '%' => '\%', '*' => '%'));


new code

if ($search_string && isset($search_params['params'])) {
        $sections = array();
        $albcat_terms = array(); // For Album & Category Title Search: populated as needed
        if ($search_params['params']['type'] == 'regex') {
                $fields = array();
                $search_string = preg_replace('/[^\w\+\*\?\{\,\}\|\(\)\\\^\$\[\]\:\<\>\-\.]/','',$search_string);
                $search_string = addslashes($search_string);
                if ($superCage->get->keyExists('album_title') || $superCage->get->keyExists('category_title')) $albcat_terms[] = " REGEXP '$search_string'";               
                foreach ($search_params['params'] as $param => $value) {
                        if (in_array($param, $allowed)) $fields[] = "$param REGEXP '$search_string'";
                }
                $sql .= count($fields) ? ('((' . implode(' OR ', $fields) . '))') : '';


// start added code for match word
}

        elseif ($search_params['params']['type'] == 'word') {
                $fields = array();
                $search_string = preg_replace('/[^\w\+\*\?\{\,\}\|\(\)\\\^\$\[\]\:\<\>\-\.]/','',$search_string);
$search_string = "[[:<:]]($search_string)[[:>:]]";
                $search_string = addslashes($search_string);

                if ($superCage->get->keyExists('album_title') || $superCage->get->keyExists('category_title')) $albcat_terms[] = " REGEXP '$search_string'";               
                foreach ($search_params['params'] as $param => $value) {
                        if (in_array($param, $allowed)) $fields[] = "$param REGEXP '$search_string'";
                }
                $sql .= count($fields) ? ('((' . implode(' OR ', $fields) . '))') : '';
// end added code for match word


         } else {
                $search_string = strtr($search_string, array('_' => '\_', '%' => '\%', '*' => '%'));



ΑndrĂ©

What's your goal? It seems that you want to enter exact 1 word in the search form and then display return just the results that match exactly that word. So a search for
Quotemale
won't return a picture that has the title/keyword/whatever
Quoteadult male

krakelis

In your example searching with 'match word' will show the picture based on the second word in the title because it's separated with a white space. I'm focusing most on keywords by the way. If I do have the two different keywords 'terrific' and 'rif' I want to be able to search for only 'rif' in a search. It is not very important but in some situations it can help. I do not have keywords with whitespaces, the few keywords with more words are coupled with a hyphen. When you search for them with 'match word' and without the hyphen you will not find them, but it's only an extra option so that is acceptable.