我的ft_min_word_len
在 MySQL 中设置为 4。当我尝试全文搜索包含 2 或 3 个字母的内容时,我没有得到任何结果。因此,我使用此代码来对这些简短单词进行全文搜索:
$table = $this->db->shop_products();
$where = '';
preg_match_all("~[\\pL\\pN_]+('[\\pL\\pN_]+)*~u", stripslashes($find), $matches);
foreach($matches[0] as $part) {
$isFirst = empty($where);
$regexp = "REGEXP '" . addslashes($part) . "'";
if(!$isFirst) $where .= " AND (";
$where .= "name {$regexp} OR content {$regexp}";
if(!$isFirst) $where .= ")";
}
return $table->where($where)->limit(5)->fetchAll();
这段代码工作正常,但它有变音符号的问题,比如 č, á, é, í...
。例如,我有名为 bé
的产品当我只想查找be
时,它不会向我显示该产品,因为它没有相同的字母。
注意:我正在使用NotORM对于 MySQL 查询,但我希望您知道 $table->where(...
是如何工作的.
最佳答案
所以我找到了解决方案,但是使用的是 PHP,而不是 MySQL。在 foreach
中,我将这一行 $part = diagriticToNormal($part);
- 这样它将把特殊字符更改为经典字符,或者如何命名它。在这个答案下有这个功能。我知道它很蹩脚,对性能不利等......但作为一个开始就足够了。
function diagriticToNormal($text) {
$charsMap = array(
'a' => array('á', 'ä'),
'c' => array('č'),
'd' => array('ď'),
'e' => array('é', 'ě'),
'i' => array('í'),
'l' => array('ľ', 'ĺ'),
'n' => array('ň'),
'o' => array('ô', 'ó'),
'r' => array('ŕ', 'ř'),
's' => array('š'),
't' => array('ť'),
'u' => array('ú', 'ů'),
'y' => array('ý'),
'z' => array('ž'),
);
foreach($charsMap as $real => $alt) {
$alt = implode('|', $alt);
$text = str_replace(array(strtolower($real), strtoupper($real)), '[' . $real . '|' . $alt . ']', $text);
}
return $text;
}
关于php - 在 MySQL (PHP) 中使用 ft_min_word_len 下的字母进行全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38095965/