PHP: çizili / kesik dize & CamelCase Needing regex

0 Cevap php

Ben bu yöntemi aşağı düzene çalışıyorum. Temelde, bir eylem (string) alır yapar bu yüzden önünde bir çizgi alırsa sonuç, bir yerli php işlevine eşdeğerdir ise kesik / CamelCase içine dizeleri altını ve sonra testler. Ben tüm bu regex olabilir düşünüyorum ama ben function_exists sınamak istiyorum nasıl emin değilim. Herhangi bir yardım büyük beğeni topluyor!

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 Cevap