Hump naming and underline naming often require mutual transfer, the following provides two ways to implement PHP.
The first method is relatively inefficient and is implemented in the following ways:
//Hump named turn underline named functionTounderscore ($str) { $dstr=Preg_replace_callback('/([a-z]+)/',function($matchs) { return‘_‘.Strtolower($matchs[0]); },$str); return Trim(Preg_replace('/_{2,}/', ' _ ',$dstr),‘_‘); } //underline name to hump name functionTocamelcase ($str) { $array=Explode(‘_‘,$str); $result=$array[0]; $len=Count($array); if($len>1) { for($i= 1;$i<$len;$i++) { $result.=Ucfirst($array[$i]); } } return $result; } Diego Link: http://www.jianshu.com/p/773fd334052fSource: Jane Book copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
The second method is more ingenious and efficient, and the second method is recommended
/** * underline to HUMP * Idea: * Step1. The original string is converted to lowercase, the delimiter in the original string is replaced with a space, and a delimiter is added at the beginning of the string * Step2. Converts the first letter of each word in the string to uppercase, then to the space, to the string header appended by the delimiter.*/ functionCamelize ($uncamelized _words,$separator= ' _ ') { $uncamelized _words=$separator.Str_replace($separator, " ",Strtolower($uncamelized _words)); return LTrim(Str_replace(" ", "",Ucwords($uncamelized _words)),$separator ); }/** * Hump named Turn underline name * idea: * Lowercase and uppercase close together place, plus delimiter, and then all turn lowercase*/ functionUncamelize ($camelCaps,$separator= ' _ ') { return Strtolower(Preg_replace('/([A-z]) ([A-z])/', "$".$separator. "$",$camelCaps)); } Diego Link: http://www.jianshu.com/p/773fd334052fSource: Jane Book copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.
Camel name and underline name Mutual transfer PHP implementation