将preg_replace转换为preg_replace_callback修饰符错误

时间:2016-04-22 09:03:38

标签: php preg-replace preg-replace-callback

我有这段代码:

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

我收到php警告(php 5.6),我尝试使用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)
        );

但是我收到了这个错误:

Modifier /e cannot be used with replacement callback 

有人可以帮我转换吗?

由于

1 个答案:

答案 0 :(得分:3)

如评论中所述,删除e修饰符,我也认为您不需要大括号。
您的代码变为:

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