Reputation: 1021
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);
Upvotes: 0
Views: 88
Reputation: 38
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;
Upvotes: 1
Reputation: 38
Try $string = preg_replace('/(c[e|é|è]ra)/i{L}', '<span class="highlight">$1</span>', $text);
I deleted \b i don't saw why that used.
Upvotes: 0