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

Ste
  • 1,497
  • 8
  • 33
  • 63
  • 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. – Rizier123 Apr 22 '16 at 09:05

1 Answers1

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)
);
Toto
  • 89,455
  • 62
  • 89
  • 125