php - MySQL正则表达式函数土耳其字符问题

标签 php mysql regex

我正在使用土耳其语的 php 进行搜索。在土耳其字母表中有“i”和“ı”小写字符。还有 'I' 和 'İ' 大写字符。

Mysql select query in regex function cannot found Turkish character, example: İ - ı .. 我的搜索列数据类型字符集UTF-8 general_ci。

我在等你的答案。提前致谢。

最佳答案

您可以通过以下方式解决土耳其语字符问题。

使用 HTML 和 JQuery 代码,如下所示;

function searchBarReplaceChar() {
        searchInput = $.trim($('input[name="replaceChar"]').val());

        let charMap = {
            Ç: '[CÇ]+',
            C: '[CÇ]+',
            Ö: '[OÖ]+',
            O: '[OÖ]+',
            Ş: '[SŞ]+',
            S: '[SŞ]+',
            İ: '[Iİ]+',
            I: '[Iİ]+',
            Ü: '[UÜ]+',
            U: '[UÜ]+',
            Ğ: '[GĞ]+',
            G: '[GĞ]+',
            ç: '[cç]+',
            c: '[cç]+',
            ö: '[oö]+',
            o: '[oö]+',
            ş: '[sş]+',
            s: '[sş]+',
            ı: '[ıi]+',
            i: '[ıi]+',
            ü: '[uü]+',
            u: '[uü]+',
            ğ: '[gğ]+',
            g: '[gğ]+',
        };

        let str_array = searchInput.split('');

        for (let i = 0, len = str_array.length; i < len; i++) {
            str_array[i] = charMap[str_array[i]] || str_array[i];
        }

        searchInput = str_array.join('');

        let replaceChar = searchInput.replace(/[]/gi, "");
        let lastChar = replaceChar.slice(-1);
        if (lastChar === '+') {
            replaceChar = replaceChar.slice(0, -1);
        }
        $('.result').text(replaceChar);
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" name="replaceChar" value="şehir bu gece çok güzel" />
<p class="result"></p>
<button type="submit" onclick="searchBarReplaceChar()" >Send</button>

使用 ajax post 将此结果发送到您的 php 文件。

稍后,您的 php 文件代码,

public function search_regexp() {
    $regexp = str_replace('i', 'İ', $_POST['get_replace_result']);
    $regexp = str_replace('ı', 'I', $regexp);
    $regexp = mb_strtoupper($regexp, "UTF-8");
    $sql_query = "SELECT * FROM your_table WHERE example_column regexp '$regexp'";
    // $sql_query = "SELECT * FROM your_table WHERE example_column regexp '[SŞ]+EH[Iİ]+R B[UÜ]+ [GĞ]+E[CÇ]+E [CÇ]+[OÖ]+K [GĞ]+[UÜ]+ZEL'";
}

不要忘记这一点,mysql数据库在你的表列数据类型字符集utf8_turkish_ci

祝你好运

关于php - MySQL正则表达式函数土耳其字符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55096839/

相关文章:

php - 通过数据透视表进行 Eloquent or 查询

php - 如何比较不同表中的两列

php并发请求卡住?

php - 可捕获的 fatal error : Object of class player could not be converted to string

php - 多个通配符 preg_match_all php

php - Laravel save() 方法未正确检测自定义主键

sql - Mysql 复制转储无需停机?空的sql文件

c# - 从英国邮政编码获取进出代码

python - 带有python正则表达式的文字括号

mysql - 如何将文件转换为带有外键的INSERT?