方法很多,几个思路:
$value = ucwords(str_replace(['-', '_'], ' ', $value));
lcfirst(str_replace(' ', '', $value));
输出结果便是:
其他方法:
/*
* 下划线转驼峰
*/
private function convertUnderline($str)
{
$str = preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){
return strtoupper($matches[2]);
},$str);
return $str;
}
/*
* 驼峰转下划线
*/
private function humpToLine($str){
$str = preg_replace_callback('/([A-Z]{1})/',function($matches){
return '_'.strtolower($matches[0]);
},$str);
return $str;
}
private function convertHump(array $data){
$result = [];
foreach ($data as $key => $item) {
if (is_array($item) || is_object($item)) {
$result[$this->humpToLine($key)] = $this->convertHump((array)$item);
} else {
$result[$this->humpToLine($key)] = $item;
}
}
return $result;
}