2

I have this code from an app in PHP 5.4 :

$rightKey = preg_replace(array(
                "/(_)(\p{L}{1})/eu",
                "/(^\p{Ll}{1})/eu"
            ), 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(
                "/(_)(\p{L}{1})/u",
                "/(^\p{Ll}{1})/u"
            ), 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 :),

1

1 Answer 1

2

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(
            "/_(\p{L})/u",
            "/(^\p{Ll})/u"
        ), 
        function($m) { 
            return mb_strtoupper($m[1], 'UTF-8');
        },
        $key);
echo $rightKey;

Output:

Abc Def
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.