I can highlight one word in string like this:
$str = "my bird is funny";
$keyword = "fun";
$str = preg_replace("/($keyword)/i","<b>$0</b>",$str);
Ant this will give me:
my bird is funny
But how to make this work when keyword is from more than one word for example when
$keyword = "bird fun";
I would like to get this result: my bird is funnny
One of the most basic concepts of regular expressions is alternation.
bird|funwill match eitherbirdorfun. This alternation can easily be generated usingimplodeandexplode:As pritaeas pointed out, you could also use
str_replace:Of course, if you write
$keywordyourself and don’t use it anywhere else then write it as a regex right away:or even
This way you don’t need any explode or implode at all.