20090423

Process search string before sending to a match against

It's Sant Jordi and I want to go home NOW!

but this peace of code will be helpful, how to process a standard search string to do a typical and search for each word.


$text=$search;

if (stripos($text, '"')===false)
{// normal search, there are no "
$text=str_replace('+', '', $text);
$text=str_replace('-', '', $text);
$text=str_replace('*', '', $text);
$text=str_replace('\'', '', $text);
$text=str_replace('"', '', $text);
$text=str_replace('>', '', $text);
$text=str_replace('<', '', $text);

$arr=explode(" ", $text);
$text='';
foreach ($arr as $elem)
{
$text.='+'.$elem.'* ';
}
}



Simply use $search as the input (obviously sanitized) and take $text as output, if there are some double quotes, we simply do nothing assuming an exact match query.

It's not perfect, but works for my actual project.

I don't have more time, happy Sant Jordi to all !

No hay comentarios: