如何在 PHP 中突出显示 utf8 文本中的非 utf8 字符串?

How to highlight non utf8 string in utf8 text in PHP?

我找不到在 PHP 中忽略 UTF8 符号来突出显示匹配项的解决方案。

代码示例:

$text = "Lorem Ipsum – tas ir teksta salikums, kuru izmanto poligrāfijā un maketēšanas darbos. Lorem Ipsum ir kļuvis par vispārpieņemtu teksta aizvietotāju kopš 16. gadsimta sākuma. Tajā laikā kāds nezināms iespiedējs izveidoja teksta fragmentu, lai nodrukātu grāmatu ar burtu paraugiem.";
$keywordsNotWorking = ["poligrafija", "kops"];
$keywordsWorking = ["poligrāfijā", "kopš"];

function highlightFoundText($text, $keywords, $tag = "b")
{
  foreach ($keyword as $key){
    $text = preg_replace("/\p{L}*?".preg_quote($key)."\p{L}*/ui", "<".$tag.">[=11=]</".$tag.">", $text);
  }
  return $text;
}

如果我使用$keywordsWorking,那么一切都可以,但是当我使用$keywordsNotWorking时,则找不到匹配的结果。 请帮助我找到解决方案,如何在忽略 UTF8 符号的情况下突出显示匹配项。

最后,我制定了可行的解决方案。 Post 回答,如果有人会去同样的问题。

class Highlighter
{
    private $_text;
    private $_keywords;

    private $keywords;
    private $text;

    private $tag = "b";

    public function highlight($text, $keywords)
    {
        $this->text = $text;
        $this->keywords = (array) $keywords;

        if(count($keywords) > 0)
        {
            $this->prepareString();
            $this->highlightStrings();
        }

        return $this->text;
    }

    private function unicodeSymbols()
    {
        return [
            'ā' => 'a',
            'č' => 'c',
            'ē' => 'e',
            'ī' => 'i',
            'ķ' => 'k',
            'ļ' => 'l',
            'ņ' => 'n',
            'š' => 's',
            'ū' => 'u',
            'ž' => 'z'
        ];
    }

    private function clearVars()
    {
        $this->_text = null;
        $this->_keywords = [];
    }

    private function prepareString()
    {
        $this->clearVars();

        $this->_text = strtolower( strtr($this->text, $this->unicodeSymbols()) );

        foreach ($this->keywords as $keyword)
        {
            $this->_keywords[] = strtolower( strtr($keyword, $this->unicodeSymbols()) );
        }
    }

    private function highlightStrings()
    {
        foreach ($this->_keywords as $keyword)
        {

            if(strlen($keyword) === 0) continue;

            // find cleared keyword in cleared text.
            $pos = strpos($this->_text, $keyword);

            if($pos !== false)
            {

                $keywordLength = strlen($keyword);

                // find original keyword.
                $originalKeyword = mb_substr($this->text, $pos, $keywordLength);

                // highlight in both texts.
                $this->text = str_replace($originalKeyword, "<{$this->tag}>".$originalKeyword."</{$this->tag}>", $this->text);
                $this->_text = str_replace($keyword, "<{$this->tag}>".$keyword."</{$this->tag}>", $this->_text);
            }

        }
    }

    public function setTag($tag)
    {
        $this->tag = $tag;
    }
}