Search code examples
phppreg-replacepreg-replace-callback

How to transform preg_replace() to preg_replace_callback() with multiples calls?


I have this line of code:

$from_uk_name = preg_replace("/^_/", "", preg_replace("/([A-Z]{1})/e", "'_'.strtolower($property_name[1])", $from_name))

But since I move the server to PHP 5.5.22 and preg_replace() is deprecated I need to move that line into preg_replace_callback() but has not idea since there are nested preg_replace calls, can I get some advice?


Solution

  • As from the comments requested:

    This should work for you:

    $from_uk_name = preg_replace("/^_/", "",
                preg_replace_callback("/([A-Z]{1})/", function($m){
                    return "'_'" . strtolower($m[1]);
                }, $from_name));