简体   繁体   English

使用 preg_replace 突出显示带重音的搜索词

[英]Highlighting an accented search term using preg_replace

I've been trying to bold a search term in a sentence.我一直试图在句子中加粗搜索词。 If the sentence is Engliš is spoken wörldwide.如果句子是Engliš is spoken wörldwide. If my search term is spoken world I want to get Engliš is <b>spoken wörld</b>wide.如果我的搜索词是spoken world我希望Engliš is <b>spoken wörld</b>wide.

I've used this function:我用过这个功能:

function highlightWords($text, $searchTerm){
   $corr = ['a' => '[aäâ]', 'o' => '[oöòóôõ]', 'c' => '[cç]', 's' => '[şśšșŝ]', 'y' => '[ýÿŷȳy]', 'o' => '[ôöòóøōoõ]', 'n' => '[ñńňn]',  'u' => '[üu]'];
   $key = preg_quote($searchTerm);
   $pattern = '/' . strtr($key, $corr) . '/iu';
   $text = preg_replace($pattern, '<b>$0</b>', $text);
   return $text;
}

It supposed to work, but I get really strange behavior.它应该可以工作,但我得到了非常奇怪的行为。 Few examples are:几个例子是:

Text is Sygmaý çykdy deşdi-sähra düzünden (sorry for the weird sentence).文本是Sygmaý çykdy deşdi-sähra düzünden (抱歉给了奇怪的句子)。 When $searchTerm is duz it perfectly works, I get Sygmaý çykdy deşdi-sähra <b>düz</b>ünden .$searchTerm duzduz它完美地工作时,我得到Sygmaý çykdy deşdi-sähra <b>düz</b>ünden If I change search term to sahra , the function returns just plain Sygmaý çykdy deşdi-sähra düzünden .如果我将搜索词更改为sahra ,则该函数仅返回简单的Sygmaý çykdy deşdi-sähra düzünden

Works with cykdy and çykdy .cykdyçykdy

But doesn't work with neither sygmay nor sygmaý .但不适用于sygmaysygmaý But works with Sygmaý with capital letter.但与大写字母Sygmaý一起使用。

What should I need to fix in order to get highlighted search term in all scenarios?为了在所有情况下都突出显示搜索词,我需要解决什么问题?

There are a couple of problems in the function, specifically in the $corr array.该函数存在一些问题,特别是在$corr数组中。

First, there are two "o" sections, and they are different.首先,有两个“o”部分,它们是不同的。 Those need to be combined.这些需要结合起来。 Second, the unaccented letter must be in the array for each letter.其次,无重音字母必须在每个字母的数组中。 s does not have this, the missing "s" is what is causing this particular failure. s 没有这个,缺少的“s”是导致这个特殊失败的原因。

Fixed function:固定功能:

function highlightWords($text, $searchTerm)
{
    $corr    = [
        'a' => '[aäâ]', 
        'o' => '[oöòóôõøō]', 
        'c' => '[cç]', 
        's' => '[sşśšșŝ]', 
        'y' => '[yýÿŷȳ]', 
        'n' => '[nñńň]', 
        'u' => '[uü]'
    ];
    $key     = preg_quote($searchTerm);
    $pattern = '/' . strtr($key, $corr) . '/iu';
    $text    = preg_replace($pattern, '<b>$0</b>', $text);
    return $text;
}

$input = 'Sygmaý çykdy deşdi-sähra düzünden';
$term  = 'sahra';

$expected = 'Sygmaý çykdy deşdi-<b>sähra</b> düzünden';

$highlighted = highlightWords($input, $term);

assert($highlighted == $expected, 'Term should be marked bold');
echo $highlighted . PHP_EOL;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM