Highlight Unicode Text in PHP

I was trying to get my old highlight function to highlight Russian text from a MySQL database. No Go. It was using eregi_replace which is deprecated according to the php manual. I ended up getting this to work:

function highlight($word, $block)
  $pattern = '/('. quotemeta($word) .')/ui';
  return preg_replace($pattern, '<span class="highlight">$1</span>' , $block );

That’s it. I use it to highlight the searched words on khristos.org like this:

$q = $_GET['q'];
foreach($results as $result)
  echo highlight($q, $result);

In the pattern line you will see a ‘/ui’ at the end. The u stands for Unicode and the i stands for case-insensitive.

Hope this helps.