php mysql search Arabic text without diacritics with Arabic text with diacritics [duplicate]

cetgtptt  于 2023-08-02  发布在  Mysql
关注(0)|答案(1)|浏览(102)

此问题在此处已有答案

Searching Arabic names discard the differences between "أ" , "ا" in mysql(2个答案)
10天前关闭。
我有一个mysql database,它包含一个带有5 columns的表
列的名称db_aya包含如下所示的Shaped字母

إِنَّ ٱلَّذِينَ كَفَرُواْ سَوَآءٌ عَلَيۡهِمۡ ءَأَنذَرۡتَهُمۡ أَمۡ لَمۡ تُنذِرۡهُمۡ لَا يُؤۡمِنُونَ

字符串
用户希望在此列上进行搜索
我有一个输入字段,用户可以键入一个或多个单词进行搜索,但用户正在写这样的不成形的字母

إن الذين كفروا سواء


并且在这种情况下,用户得到0个结果
如何解决这个问题时,用户输入unshaped字母得到的结果,如果在数据库中存在注意,在上面的例子中,这两个是相似的,但一个是形状,另一个不是
我试着写了这段代码:

<?php
include_once "../includes/connect.php";
$header = "content-type: application/json; charset=utf-8";
$search = mysqli_real_escape_string($conn, $_GET['search']) ;

$response = array();
// Check connection
if ($conn) {
    $sql = mysqli_query($conn, "select db_id, db_aya from tbl_khire where db_aya LIKE '%$search%' order by db_id asc") or die(mysqli_error($conn));
    if ($sql) {
        header($header);
        $i = 0;
        while ($row = mysqli_fetch_array($sql)) {
            $response[$i]['aya'] = $row['db_aya'];
            echo $row['db_aya'];
            $i++;
        }
        $json = json_encode($response, JSON_UNESCAPED_UNICODE);
        echo $json;
    }
} else {
    echo "database connection failed";
}
?>

axzmvihb

axzmvihb1#

经过大量的尝试,我用这段代码解决了这个问题
首先,我从输入中删除所有的附加符号
然后使用REPLACE通过sql查询删除它们。
验证码:

<?php
include_once "../includes/connect.php";
$header = "content-type: application/json; charset=utf-8";
$response = array();
$searchValue = mysqli_real_escape_string($conn, $_GET['search']);
// List of characters to remove (diacritics and other symbols)
$charactersToRemove = array('َ', 'ُ', 'ِ', 'ً', 'ٌ', 'ٍ', 'ّ', 'ۖ', 'ۗ', 'ۘ', 'ۚ', 'ۛ', '۠', 'ۡ', 'ۢ', 'ۦ', 'ۧ', 'ۨ', '۪', '۫', '۬', 'ء');

// Remove characters from user input
$userInputWithoutDiacritics = str_replace($charactersToRemove, '', $searchValue);

// Check connection
if ($conn) {
    $sql = mysqli_query($conn, "SELECT * FROM tbl_khire WHERE
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(db_aya, 'َ', ''), 'ُ', ''), 'ِ', ''), 'ً', ''), 'ٌ', ''), 'ٍ', ''), 'ّ', ''), 'ۖ', ''), 'ۗ', ''), 'ۘ', ''), 'ۚ', ''), 'ۛ', ''), '۠', ''), 'ۡ', ''), 'ۢ', ''), 'ۦ', ''), 'ۧ', ''), 'ۨ', ''), '۪', ''), '۫', ''), '۬', ''),'ء','')
    LIKE CONCAT('%', REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
    '$userInputWithoutDiacritics', 'َ', ''), 'ُ', ''), 'ِ', ''), 'ً', ''), 'ٌ', ''), 'ٍ', ''), 'ّ', ''), 'ۖ', ''), 'ۗ', ''), 'ۘ', ''), 'ۚ', ''), 'ۛ', ''), '۠', ''), 'ۡ', ''), 'ۢ', ''), 'ۦ', ''), 'ۧ', ''), 'ۨ', ''), '۪', ''), '۫', ''), '۬', ''),'ء',''), '%')") or die(mysqli_error($conn));

    if (mysqli_num_rows($sql) > 0) {
        header($header);
        $i = 0;
        while ($row = mysqli_fetch_array($sql)) {
            $response[$i]['aya'] = $row['db_aya'];
            $i++;
        }
        $json = json_encode($response, JSON_UNESCAPED_UNICODE);
        echo $json;
    }
} else {
    echo "database connection failed";
}

字符串
这段代码帮助我做了一个搜索与阿拉伯字母包含变音符号从用户或在数据库中

相关问题