包含所有字母的正则表达式单词
Regex word with all letters
我正在尝试编写一些正则表达式来匹配用任何字符书写的单词并使用 preg_replace
突出显示它们
示例:
Search "cera" in "my sentence which contain cera, or Céra CErA or even cERa"
Return 预计:
my sentence which contain <span class="highlight">cera</span>, or <span class="highlight">Céra</span> and <span class="highlight">CErA</span> or even <span class="highlight">cERa</span>"
这是我的正则表达式,但它不起作用?!
$string = preg_replace('\bcera\b\p{L}', '<span class="highlight"></span>', $text);
尝试$string = preg_replace('/(c[e|é|è]ra)/i{L}', '<span class="highlight"></span>', $text);
我删除了 \b 我不明白为什么要用它。
此函数在 return 正则表达式搜索时查找值。
$text 和 $search 设置用于演示
function txt_replace($search) {
mb_regex_encoding('UTF-8');
$match=Array("aáàâäåãAÁÀÂÄÅÃ","cçCÇ","eéèêëEÉÈÊË","iíìîïIÍÌÎÏ","nñNÑ","oóòôöõOÓÒÔÖÕ","uúùûüUÚÙÛÜ","yÿýYÝ","æÆ", "ɶŒœ");
foreach($match as $key => $value) {
$search=mb_ereg_replace("([$value])", "[$value]*", $search);
}
return "({$search})";
}
$text="sàlut bonjour new york salut";
$search="sàlut";
$string=preg_replace('/'.txt_replace($search).'/i','<span class="highlight"></span>', $text);
echo $string;
我正在尝试编写一些正则表达式来匹配用任何字符书写的单词并使用 preg_replace
突出显示它们示例:
Search "cera" in "my sentence which contain cera, or Céra CErA or even cERa"
Return 预计:
my sentence which contain <span class="highlight">cera</span>, or <span class="highlight">Céra</span> and <span class="highlight">CErA</span> or even <span class="highlight">cERa</span>"
这是我的正则表达式,但它不起作用?!
$string = preg_replace('\bcera\b\p{L}', '<span class="highlight"></span>', $text);
尝试$string = preg_replace('/(c[e|é|è]ra)/i{L}', '<span class="highlight"></span>', $text);
我删除了 \b 我不明白为什么要用它。
此函数在 return 正则表达式搜索时查找值。 $text 和 $search 设置用于演示
function txt_replace($search) {
mb_regex_encoding('UTF-8');
$match=Array("aáàâäåãAÁÀÂÄÅÃ","cçCÇ","eéèêëEÉÈÊË","iíìîïIÍÌÎÏ","nñNÑ","oóòôöõOÓÒÔÖÕ","uúùûüUÚÙÛÜ","yÿýYÝ","æÆ", "ɶŒœ");
foreach($match as $key => $value) {
$search=mb_ereg_replace("([$value])", "[$value]*", $search);
}
return "({$search})";
}
$text="sàlut bonjour new york salut";
$search="sàlut";
$string=preg_replace('/'.txt_replace($search).'/i','<span class="highlight"></span>', $text);
echo $string;