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);
Now, the request word could be something like "josé". And with it, I want "jose" or "JOSÉ" or "José" etc highlighted too.
现在,请求单词可能类似于“josé”。有了它,我也想要“jose”或“JOSÉ”或“José”等。
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.
有了这个表达式,如果我写“josé”,它会匹配“josé”和“JOSÉ”(以及所有案例变体)。它始终只与重音变体匹配。如果我搜索“jose”,它会匹配“JOSE”,“jose”,“Jose”但不匹配重音符号。所以我部分地想要什么,因为我对重音和非重音分别不区分大小写。
I need it fully combined, wich means accent (unicode) insensitive, so I can search "jose", and highlight "josé", "josÉ", "José", "JOSE", "JOSÉ", "JoSé", ...
我需要它完全组合,这意味着口音(unicode)不敏感,所以我可以搜索“jose”,并突出显示“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.
我不想在单词上替换重音,因为当我在屏幕上打印它时,我需要看到真实的单词。
Any ideas?
Thanks!
2 个解决方案
#1
9
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:
您可以尝试创建一个函数来创建基于txt_search的正则表达式,将所有可能的匹配替换为所有可能的匹配,如下所示:
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;
}
Then you use the result as a regex on your preg_replace.
然后将结果用作preg_replace上的正则表达式。
#2
1
You might have to parse the search string, and modify the pattern in the regex so that if includes cases like [eéÉ]. Replace all instances of e/E/é/É with a catch-all [eEéÉ]. Do the same for all other cases. So in your example the search pattern, instead of Jose/José/JOSÉ, would be jos[éÉeE]
您可能必须解析搜索字符串,并修改正则表达式中的模式,以便包含[eéÉ]等案例。用全能[eEéÉ]替换所有e / E /é/É实例。对所有其他情况也这样做。所以在你的例子中,搜索模式,而不是Jose /José/JOSÉ,将是jos [éÉeE]
#1
9
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:
您可以尝试创建一个函数来创建基于txt_search的正则表达式,将所有可能的匹配替换为所有可能的匹配,如下所示:
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;
}
Then you use the result as a regex on your preg_replace.
然后将结果用作preg_replace上的正则表达式。
#2
1
You might have to parse the search string, and modify the pattern in the regex so that if includes cases like [eéÉ]. Replace all instances of e/E/é/É with a catch-all [eEéÉ]. Do the same for all other cases. So in your example the search pattern, instead of Jose/José/JOSÉ, would be jos[éÉeE]
您可能必须解析搜索字符串,并修改正则表达式中的模式,以便包含[eéÉ]等案例。用全能[eEéÉ]替换所有e / E /é/É实例。对所有其他情况也这样做。所以在你的例子中,搜索模式,而不是Jose /José/JOSÉ,将是jos [éÉeE]