【问题标题】:Searching for UTF-8 channels without UTF-8 letters in query在查询中搜索没有 UTF-8 字母的 UTF-8 通道
【发布时间】:2021-01-08 15:17:45
【问题描述】:

当 slug 包含 UTF8 字母时,获得非 utf8 结果的最佳方法是什么? (例如:当我输入slug“Zaidimai”时,从数据库中选择所有类似“Žaidimai”和“Zaidimai”的地方)。

当前代码:

$results = DB::table('channels') -> where([['name', 'like', '%' . $slug . '%'], ['status', '=', 'OK']]) -> orWhere([['slug', 'like', '%' . $slug . '%'], ['status', '=', 'OK']]) -> limit(3) -> get();

它工作正常,但是当我尝试通过在搜索栏中输入“Zaidimai”来搜索频道时,它没有显示我想要的频道(Žaidimai)。

【问题讨论】:

    标签: php mysql laravel postgresql laravel-5


    【解决方案1】:

    我的解决方案是在 mysql 中创建这个 slugify 过程,并在 where 子句中调用它

    DELIMITER ;;
    
    DROP FUNCTION IF EXISTS `slugify`;;
    CREATE FUNCTION `slugify`(dirty_string varchar(255)) RETURNS varchar(255) CHARSET utf8
        DETERMINISTIC
    BEGIN
        DECLARE x, y , z , k INT;
        DECLARE temp_string, new_string, accents, noAccents VARCHAR(255);
        DECLARE is_allowed BOOL;
        DECLARE c, check_char VARCHAR(1);
    
        -- IF NULL DO NOT PROCEED
        If dirty_string IS NULL Then
            return dirty_string;
        End If;
    
        set temp_string = LOWER(dirty_string);
    
        -- REPLACE ACCENTS
        -- WITH CAPS
        -- set accents = 'ŠšŽžÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÑÒÓÔÕÖØÙÚÛÜÝŸÞàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿþƒ';
        -- set noAccents = 'SsZzAAAAAAACEEEEIIIINOOOOOOUUUUYYBaaaaaaaceeeeiiiinoooooouuuuyybf';
        -- ONLY SMALL CAPS
        set accents = 'šžàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿþƒ';
        set noAccents = 'szaaaaaaaceeeeiiiinoooooouuuuyybf';
        set k = CHAR_LENGTH(accents);
    
        while k > 0 do
            set temp_string = REPLACE(temp_string, SUBSTRING(accents, k, 1), SUBSTRING(noAccents, k, 1));
            set k = k - 1;
        end while;
    
        -- CONVERT & TO EMPTY SPACE
        Set temp_string = REPLACE(temp_string, '&', '');
    
        -- REPLACE ALL UNWANTED CHARS
        Select temp_string REGEXP('[^a-z0-9\-]+') into x;
        If x = 1 then
            set z = 1;
            set k = CHAR_LENGTH(temp_string);
            While z <= k Do
                Set c = SUBSTRING(temp_string, z, 1);
                Set is_allowed = FALSE;
                If !((ascii(c) = 45) or (ascii(c) >= 48 and ascii(c) <= 57) or (ascii(c) >= 97 and ascii(c) <= 122)) Then
                    Set temp_string = REPLACE(temp_string, c, '-');
                End If;
                set z = z + 1;
            End While;
        End If;
    
        Select temp_string REGEXP("^-|-$|'") into x;
        If x = 1 Then
            Set temp_string = Replace(temp_string, "'", '');
            Set z = CHAR_LENGTH(temp_string);
            Set y = CHAR_LENGTH(temp_string);
            Dash_check: While z > 1 Do
                If STRCMP(SUBSTRING(temp_string, -1, 1), '-') = 0 Then
                    Set temp_string = SUBSTRING(temp_string,1, y-1);
                    Set y = y - 1;
                Else
                    Leave Dash_check;
                End If;
                Set z = z - 1;
            End While;
        End If;
    
        Repeat
            Select temp_string REGEXP("--") into x;
            If x = 1 Then
                Set temp_string = REPLACE(temp_string, "--", "-");
            End If;
        Until x <> 1 End Repeat;
    
        If LOCATE('-', temp_string) = 1 Then
            Set temp_string = SUBSTRING(temp_string, 2);
        End If;
    
        Return temp_string;
    END;;
    
    DELIMITER ;
    

    在你选择你

    select * from channels where LOWER('Zaidimai') = slugify(`slug`)...
    

    【讨论】:

      【解决方案2】:

      安装'unaccent' Postgres 扩展,然后使用带有unaccent 函数的原生SQL。 :slug 是下面查询中的参数。

      select <whatever you need>
        from channels
        where (unaccent("name") ilike '%'||unaccent(:slug)||'%' or unaccent(slug) ilike '%'||unaccent(:slug)||'%')
        and status = 'OK'
        limit 3;
      

      【讨论】:

        猜你喜欢
        • 2014-03-10
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 2017-10-28
        • 1970-01-01
        • 1970-01-01
        • 2012-02-02
        相关资源
        最近更新 更多