php - 在 MySQL (PHP) 中使用 ft_min_word_len 下的字母进行全文搜索

标签 php mysql

我的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();

这段代码工作正常,但它有变音符号的问题,比如 č, á, é, í... 。例如,我有名为 的产品当我只想查找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/

相关文章:

php - 如何将多个表链接到一个查询中

javascript - 在 AJAX 处理程序中下载文件

mysql - 在 VB.net 中打印和存储收据

mysql order by,首先显示大写结果

php - 输入字段占位符不起作用。?

php - 在 php 中缓存文件的最佳方法是什么?

mysql - 无法在 Mariadb 中创建主键

mysql - 应用程序设计单个数据库与多个数据库

php - 不插入数据库。 PHP 和 MySQL

php - 如何使用 PHP 和远程 ssh 从文本文件中获取信息?