使用 javascript 完成此操作的一种简单方法是:
var apart = "Some +example search that I made up".toLowerCase().replace(/[\+\-\?]*/g, '').split(' '),
stop_words = ['the', 'that', 'a', 'example'],
min_word_length = 1;
// filter the array to remove stop words
apart.filter( function( item ) {
if ( item.length < min_word_length ) return false;
return stop_words.indexOf( item ) === -1;
});
编辑:虽然在伪代码意义上,上面的代码可以工作,这是一个 PHP 解决方案
$query = "This is my example search query";
$search = explode(' ', $query);
$stop_words = array('is', 'a', 'the');
$terms = array_filter($search, function($item){
return !in_array($item, $stop_words);
});
$formatted = array_map(function($term){
return '+'.mysql_escape_string($term).'*';
}, $terms);
// :keyStr = implode(' ', $formatted);