简体   繁体   中英

Regex word with all letters

I'm trying to write some regex to match word which is writen with any characters and highlights them using preg_replace

Example :

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

Return expected :

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>"

Here is my regex but it doesn't work any ideas ?!

$string = preg_replace('\bcera\b\p{L}', '<span class="highlight">$1</span>', $text);

This function while return the regex search for looking for the value. $text and $search set 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;

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

I deleted \\bi don't saw why that used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM