我正试图简化这种方法。它基本上接受一个动作(字符串),将虚线/下划线字符串变成camelCase,然后测试结果是否等同于本机php函数,如果是这样,它在前面得到一个下划线。我认为这一切都可能是一个正则表达式,但我不确定我是如何测试function_exists的。非常感谢任何帮助!
public function getMethod($action){
if (strpos($action, '-') !== false){
$action = str_replace(' ', '', ucwords(str_replace('-', ' ', $action)));
$action = lcfirst($action);
}
if (strpos($action, '_') !== false){
$action = str_replace(' ', '', ucwords(str_replace('_', ' ', $action)));
$action = lcfirst($action);
}
// resolves native function names with underscore
if (function_exists($action)) return "_".$action;
else if ($action == 'list') return '_list';
else if ($action == 'new') return '_new';
else if ($action == '') return 'index';
else return $action;
}
答案 0 :(得分:0)
经过一些研究和工作后,我得出了这样的结论:
public function getMethod($action){
$pattern = array('/_(.?)/e', '/-(.?)/e', '/ (.?)/e');
$action = preg_replace($pattern, 'strtoupper("$1")', $action);
if (function_exists($action) || $action == 'list' || $action == 'new' || $action == 'sort')
return '_'.$action;
else if ($action == '')
return 'index';
else
return $action;
}