所有字母的正则表达词

时间:2015-03-11 19:41:21

标签: php regex preg-replace

我正在尝试编写一些正则表达式来匹配用任何字符写入的单词并使用preg_replace突出显示它们

示例:

Search "cera" in "my sentence which contain cera, or Céra CErA or even cERa"

预计返回:

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">$1</span>', $text);

2 个答案:

答案 0 :(得分:1)

此函数返回正则表达式搜索以查找值。 $ text和$ search for demo

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">$1</span>', $text);
echo $string;

答案 1 :(得分:0)

尝试$string = preg_replace('/(c[e|é|è]ra)/i{L}', '<span class="highlight">$1</span>', $text);

我删除了\ b我不明白为什么使用它。