preg_replace, preg_replace_callback and Array to string conversion


I have this code from an app in PHP 5.4 :

$rightKey = preg_replace(array(
            ), array(
                "mb_strtoupper('\\2', 'UTF-8')",
                "mb_strtoupper('\\1', 'UTF-8')"
            ), $key);

It didn't work well, because preg_replace is deprecated. I did some researches and turned it into :

$rightKey = preg_replace_callback(array(
            ), function($m) { return array(
                "mb_strtoupper('\\2', 'UTF-8')",
                "mb_strtoupper('\\1', 'UTF-8')"
            ); }, $key);

I changed the function to preg_replace_callback, I removed the "e", and I added a callback.

But now I have :

Array to string conversion

And, I really don't know how to adapt the callback so it works ^^.

Thanks :),

Show source
| php   | zend-framework   | regex   | preg-replace   | preg-replace-callback   2017-01-04 12:01 1 Answers

Answers ( 1 )

  1. 2017-01-04 17:01

    The function must return a string, not an array, it is the same function for every matches:

    $key = 'abc _def';
    $rightKey = preg_replace_callback(array(
            function($m) { 
                return mb_strtoupper($m[1], 'UTF-8');
    echo $rightKey;


    Abc Def
◀ Go back