Search code examples
phppreg-replacepreg-replace-callback

Replace deprecated preg_replace with preg_replace_callback


code below gives a deprecation warning after upgrading to PHP 5.5+

$sentence=preg_replace('/~([^<>]{1,})~/e', "'<span class=\"helpstart\">'.UTF8_strtoupper('\\1').'</span>'", $sentence);

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in ..

How can I replace the code with preg_replace_callback()?


Solution

  • $sentence=preg_replace('/~([^<>]{1,})~/', function($match) {return "<span class=\"helpstart\">".UTF8_strtoupper($match)."</span>"; } , $sentence);
    

    as per http://www.php.net/manual/en/function.preg-replace-callback.php