Remove Common Words from Search String
Before sending the user given search string to MySql, its a good idea to remove common words used in it so that it may not disturb any relevant results you have in your DB. Their are far more professional approaches out there for this task but here I am mentioning a very simple one.
Function:
function RemoveCommonWords($sText){
$CommonWords = array (
'at',
'the',
'and',
'of',
'in'
);
for ($x = 0; $x < count($CommonWords); $x++) {
$sText = str_replace(' ' . $CommonWords[$x] . ' ', ' ', $sText);
}
return $sText;
}
Usage:
$search_string = "sports shops in market";
$search_string = RemoveCommonWords($search_string);
Build your own list in the function against the variable $CommonWords.
Good Luck :)
Function:
function RemoveCommonWords($sText){
$CommonWords = array (
'at',
'the',
'and',
'of',
'in'
);
for ($x = 0; $x < count($CommonWords); $x++) {
$sText = str_replace(' ' . $CommonWords[$x] . ' ', ' ', $sText);
}
return $sText;
}
Usage:
$search_string = "sports shops in market";
$search_string = RemoveCommonWords($search_string);
Build your own list in the function against the variable $CommonWords.
Good Luck :)
3 Comments:
Simple and cute. Helped me in saving time on this part.
Thanks man!
By Anonymous, At September 29, 2009 at 12:08 PM
your code fails for this case :
$string = "and and and and";
Use this :
$sText = str_replace(' ' . $CommonWords[$x] , ' ', $sText);
in your code instead.
By Sowrabh, At September 29, 2009 at 12:58 PM
Worked great, thanks again!
By Unknown, At January 26, 2010 at 9:36 PM
Post a Comment
Subscribe to Post Comments [Atom]
<< Home