Highlight search results - whole expressions and single words <p>I want highlight whole expresions and single words</p> <pre><code>$text ="text text aaa bbb ccc text aaa text xxaaayy text bbb ccc text bbb cccxxx text"; $words = array('aaa bbb ccc','aaa bbb','bbb ccc','aaa','bbb','ccc'); foreach ($words as $k=&gt;$v){ $text = preg_replace('/(\w*?'.$v.'\w*)/i', '[b]$1[/b]', $text); } </code></pre> <p>this code will return:</p> <p><strong>text text [b][b]aaa[/b] [b]bbb[/b] [b]ccc[/b][/b] text [b]aaa[/b] text [b]xxaaayy[/b] text [b][b]bbb[/b] [b]ccc[/b][/b] text [b][b]bbb[/b] [b]cccxxx[/b][/b] text</strong></p> <p>how to get this result:</p> <p><strong>text text [b]aaa bbb ccc[/b] text [b]aaa[/b] text [b]xxaaayy[/b] text [b]bbb ccc[/b] text [b]bbb cccxxx[/b] text</strong></p> <p>how to modify preg_replace ?</p>