簡體   English   中英

突出顯示匹配的關鍵字,不帶重音

[英]Highlight matched keywords without accented letter

我已將此代碼用於搜索功能。 目前運作良好。 但是,如果鍵入的關鍵字不帶重音字母,則該單詞不會突出顯示。

示例:$ text =“ iphonemới”

如果我輸入關鍵字“ moi”,則不會在$ text中突出顯示單詞“mới”。 我也嘗試過將u標志作為Google的建議作為模式中的標志,但它也不起作用。 請幫忙...

這是我的代碼:

<?php
function highlightWords($text, $words)
{
/*** loop of the array of words ***/
foreach ($words as $word)
{
        /*** quote the text for regex ***/
        $word = preg_quote($word);
        /*** highlight the words ***/
        $text = preg_replace("/($word)/ui", '<span class="highlight_word">\1</span>', $text);
}
/*** return the text ***/
return $text;
}


/*** example usage ***/
$text = 'this is my iphone mới';
/*** an array of words to highlight ***/
$words = array('moi');
/*** highlight the words ***/
$highlighttext =  highlightWords($string, $words);

?>

<html>
<head>
<title>PHPRO Highlight Search Words</title>
<style type="text/css">
.highlight_word{
background-color: pink;
}
</style>
</head>
<body>
 <?php echo $highlighttext; ?>

您可以使用以下代碼:

setlocale(LC_ALL, 'en_US');
$word = iconv("utf-8","ascii//TRANSLIT",$word);
/*** quote the text for regex ***/

這將從$word刪除所有變音符號。

然后,您可以遍歷每個字符並將其替換為字符類(如果字符具有其他形式)。 例如, o成為[ớọơờớòo]

注意:nhahtdh是一個很好的觀點。 我不知道這些變音符號來自什么語言,更不用說任何字符應該變成具有不同變音符號的字符了。

如果您想沿着這條線做某事,請不要展平$word並添加重音符號規則,例如: ơ變成[ờớơ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM