Ste
Ste

Reputation: 1520

convert preg_replace to preg_replace_callback modifier error

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

Upvotes: 2

Views: 3185

Answers (1)

Toto
Toto

Reputation: 91385

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)
);

Upvotes: 4

Related Questions