php 搜索突出显示字符串,包括阿拉伯语变音字符和不包括阿拉伯语变音字符

shstlldc  于 2023-04-19  发布在  PHP
关注(0)|答案(1)|浏览(123)

是否有一个正则表达式来匹配一个包含或不包含特殊字符的特定字符串?
Like sql database keywords=ال ح مد will match after search highlight=ال ح م د
编辑:我想匹配带有和不带有特殊/重音字符的特定字符串。不仅仅是任何字符串/字符。

function mbStringToArray($string)
    {
        $strlen = mb_strlen($string);
        while($strlen)
        {
            $array[] = mb_substr($string, 0, 1, "UTF-8");
            $string = mb_substr($string, 1, $strlen, "UTF-8");
            $strlen = mb_strlen($string);
        }
        return $array;
    }

    // I had to use this ugly function to remove accents as iconv didn't work properly on my test server.
    function stripAccents($stripAccents){
        return utf8_encode(strtr(utf8_decode($stripAccents),utf8_decode('ِ', 'ُ', 'ٓ', 'ٰ', 'ْ', 'ٌ', 'ٍ', 'ً', 'ّ', 'َ'),''));
    }

    $keywords= 'الحمد';

    $search = '%' . $keywords . '%';
    $stmt = $mysqli->prepare("SELECT * FROM quran WHERE $trans LIKE ?");
    $stmt->bind_param('s', $search);
    $stmt->execute();
    $stmt->store_result();  

    $clientNameNoAccent = stripAccents($clientName);

    $clientNameArray = mbStringToArray($clientName);

    foreach($clientNameArray as $pos => &$char)
    {
        $charNA =$clientNameNoAccent[$pos];
        if($char != $charNA)
        {
            $char = "(?:$char|$charNA|$charNA\p{M})";
        }
    }

    $clientSearchPattern = implode($clientNameArray); // c(?:é|e|e\p{M})ra

    if($stmt->execute()){
            $result = $stmt->get_result();
            while($row = $result->fetch_assoc()){   
            $text = 'الْحَمْدُ لِلَّهِ رَبِّ الْعَالَمِينَ';
            $search = preg_replace('/(.*?)(' . $clientSearchPattern . ')(.*?)/iu', '$1<span class="highlight">$2</span>$3', $text);         
        echo $search;// لِلَّهِ رَبِّ الْعَالَمِينَ<span class="highlight">الْحَمْدُ</span>
        }
    }
4xrmg8kj

4xrmg8kj1#

我创建了一个方法来为关键字(由用户输入)生成动态正则表达式模式,以考虑阿拉伯语或波斯语(波斯语)口音(diacritics/tashkeel marks/eraab)。这可以用于突出显示字符串中的关键字和/或基于关键字在搜索结果中显示的长文本。我希望这对某些人有帮助!

private function generateRegexPattern(string $keyword): string
    {
        $pattern = '';

        // Convert the keyword to an array of characters
        $letters = preg_split('//u', $keyword, -1, PREG_SPLIT_NO_EMPTY);

        $lastLoop = array_key_last($letters);

        // Loop through each character in the keyword
        foreach ($letters as $key => $letter) {

            // Check if the character is an Arabic or Persian letter
            if (preg_match('/ا|أ|إ/u', $letter)) {
                // If it is, add a character class with the letter and any possible variations
                $pattern .= "(ا|أ|إ)";
            } elseif (preg_match('/ؤ|و/u', $letter)) {
                $pattern .= "(ؤ|و)";
            } elseif (preg_match('/ة|ه/u', $letter)) {
                $pattern .= "(ة|ه)";
            } else {
                // If it's not, add the character as-is
                $pattern .= $letter;
            }

            ($key !== $lastLoop) ? $pattern .= '[\x{064B}-\x{0655}]{0,2}' : $pattern .= '';
        }

        return $pattern;

    }

相关问题