本文介绍了PHP-REGEX:重音字母与非重音字母匹配,反之亦然.如何实现呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要编写典型的突出显示代码.所以我有类似的东西:

I want to do typical highlight code. So I have something like:

$valor = preg_replace("/(".$_REQUEST['txt_search'].")/iu", "<span style='background-color:yellow; font-weight:bold;'>\\1</span>", $valor);

现在,请求词可能类似于josé".与此同时,我也要突出显示"jose"或JOSÉ"或José"等.

Now, the request word could be something like "josé". And with it, I want "jose" or "JOSÉ" or "José" etc highlighted too.

使用此表达式,如果我写josé",它将匹配josé"和JOSÉ"(以及所有大小写变体).它始终仅与重音变体匹配.如果我搜索"jose",则它匹配"JOSE","jose","Jose",但不匹配重音符号.所以我已经部分满足了我的要求,因为我对重音和不重音分别不区分大小写.

With this expression, if I write "josé", it matches "josé" and "JOSÉ" (and all the case variants). It always matches the accented variants only. If I search "jose", it matches "JOSE", "jose", "Jose" but not the accented ones. So I've partially what I want, cause I have case insensitive on accented and non-accented separately.

我需要将其完全组合,这意味着对重音(unicode)不敏感,因此我可以搜索"jose",并突出显示josé",josÉ",José","JOSE",JOSÉ",JoSé" ,...

I need it fully combined, wich means accent (unicode) insensitive, so I can search "jose", and highlight "josé", "josÉ", "José", "JOSE", "JOSÉ", "JoSé", ...

我不想替换单词上的重音符号,因为当我在屏幕上打印该单词时,我需要看到真正的单词.

I don't want to do a replace of accents on the word, cause when I print it on screen I need to see the real word as it comes.

有什么想法吗?

谢谢!

推荐答案

您可以尝试根据txt_search创建一个函数来创建正则表达式,将所有可能的匹配替换为所有可能的匹配,例如:

You can try to make a function to create your regex expression based on your txt_search, replacing any possible match to all possible matches like this:

function search_term($txt_search) {
    $search = preg_quote($txt_search);

    $search = preg_replace('/[aàáâãåäæ]/iu', '[aàáâãåäæ]', $search);
    $search = preg_replace('/[eèéêë]/iu', '[eèéêë]', $search);
    $search = preg_replace('/[iìíîï]/iu', '[iìíîï]', $search);
    $search = preg_replace('/[oòóôõöø]/iu', '[oòóôõöø]', $search);
    $search = preg_replace('/[uùúûü]/iu', '[uùúûü]', $search);
    // add any other character

    return $search;
}

然后将结果用作preg_replace上的正则表达式.

Then you use the result as a regex on your preg_replace.

这篇关于PHP-REGEX:重音字母与非重音字母匹配,反之亦然.如何实现呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:20