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.phpIn 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('_' => '\_', '%' => '\%', '*' => '%'));