You could use the preg_replace_callback() function from the PCRE family. You can use the u modifier to support UTF-8.
Please note there are some smaller differences between the PCRE (preg_) and POSIX (ereg_) way, besides that the latter is deprecated.