2

i have this code:

$key = preg_replace(
            '/(^|[a-z])([A-Z])/e', 
            'strtolower(strlen("\\1") ? "\\1_\\2" : "\\2")',
            substr($method, 3) 
          );

I receive php warning ( php 5.6 ), and i try to convert it with preg_replace_callback:

$key = preg_replace_callback(
            '/(^|[a-z])([A-Z])/e',
            function($m) { 
                return strtolower(strlen("\\{$m[1]}") ? "\\{$m[1]}_{$m[2]}" : "\\{$m[2]}"); 
            },
            substr($method, 3)
        );

but i receive this error:

Modifier /e cannot be used with replacement callback 

Can someone help me to convert it right?

Thanks

1
  • 1) remove the modifier e when you use preg_replace_callback() 2) Do a simple print_r($m); in the anonymous function and see what you have there. Then you can use $m as a normal array as you would normally do. Commented Apr 22, 2016 at 9:05

1 Answer 1

4

As said in comments, remove the e modifier, I also think that you don't need the curly brackets.
Your code becomes:

$key = preg_replace_callback(
       '/(^|[a-z])([A-Z])/',
       function($m) { 
           return strtolower(strlen($m[1]) ? "$m[1]_$m[2]" : $m[2]); 
       },
       substr($method, 3)
);
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.