By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,718 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,718 IT Pros & Developers. It's quick & easy.

regexp, mysql and plural marking

P: n/a
hello all,

for a search engine, I'm looking for a regexp than can do the job
it is just (!) a 's' problem (frequent in french) :

I have a field 'thesaurus' in a table MySQL (3.23.56) ;

I have a field 'search' in a form where user can input a word ;

what I want is :

if search == 'mil'
return 'mil' (if exist) AND 'mils' (if exist),
but do not return 'military', 'million','milsblabla',...

if search == 'mils'
return 'mil' (if exist) AND 'mils' (if exist)
but do not return 'military', 'million','milsblabla',...

thank you for help or suggestion.

--
@@@@@
E -00
' `)
|\_ =="
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
denisb <ne***@no-log.org> wrote:
for a search engine, I'm looking for a regexp than can do the job
it is just (!) a 's' problem (frequent in french) :
I have a field 'thesaurus' in a table MySQL (3.23.56) ;
I have a field 'search' in a form where user can input a word ;
what I want is :
if search == 'mil'
return 'mil' (if exist) AND 'mils' (if exist),
but do not return 'military', 'million','milsblabla',...
if search == 'mils'
return 'mil' (if exist) AND 'mils' (if exist)
but do not return 'military', 'million','milsblabla',...


no answers...
so I do that :

// initialise WHERE condition for mysql query
$what = "";
// user can search all words or at least one of them
$choice = (($andor == "and") ? "AND" : "OR");
// no html
$search = str_replace(array("&quot;","&lt;","&gt;","&amp;"),
array("","","",""),
$search);
// no accent (french site)
$search = strtolower(strtr($search,
"",
"aaaaaaaaaaeeeeeiiiiooooooouuuuuyccnn"));
// only >=4 digits numbers
$search = ereg_replace("[[:blank:]][[0-9]{1,3}]+[[:blank:]]"," ",
ereg_replace("[^[:alnum:]]+"," ",
$search));
// only >=3 letters words
$search = ereg_replace("[[:blank:]][^ ]{1,2}[[:blank:]]"," ",
" ".$search." ");
// separators in thesaurus
$search = trim(ereg_replace("[[:blank:]]+","#",$search));
// make WHERE condition
$search_list = array();
$search_list = explode("#",$search);
for ($i = 1, $n = count($search_list); $i < $n-1; $i++) {
$plural = strrchr($search_list[$i], "s");
if ($plural == 's') {
$singular = substr($search_listl[$i], 0,
(strlen($search_list[$i]) - 1));
$what .= "((thesaurus LIKE '%#".$search_list[$i]."#%') OR
(thesaurus LIKE '%#".$ singular."#%'))".
(($i != $n-2) ? " ".$choice." " : "");
} else {
$what .= "((thesaurus LIKE '%#".$search_list[$i]."#%') OR
(thesaurus LIKE '%#".$search_list[$i]."s#%'))".
(($i != $n-2) ? " ".$choice." " : "");
}
}

and now :

==> "SELECT aaa, bbb, ccc FROM ttt WHERE (".$what.") ORDER BY aaa DESC"

it works for me, but I stay sure there is more simple...
--
@@@@@
E -00 it is too hard (for me) to write english message !
' `) /
|\_ =="
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.