下划线式转化为驼峰字符串
/**
* 下划线转驼峰 字符串转化函数 _make_by_id_ => MakeById
*
* @param $str
*
* @return string $str string 输出转化后的字符串
*/
function underLineToHump($str){
$str = trim($str,'_');//去除前后下划线_
$len = strlen($str);
$out = strtoupper($str[0]);
for ($i=1; $i<$len; $i++) {
if(ord($str[$i]) == ord('_')){//如果当前是下划线,去除,并且下一位大写
$out .= isset($str[$i+1])?strtoupper($str[$i+1]):'';
$i++;
}else{
$out .= $str[$i];
}
}
return $out;
}
驼峰式转化为下划线字符串
/**
* 驼峰转下划线 字符串函数 MakeById => make_by_id
* @param $str
*
* @return string
*/
function humpToUnderLine($str)
{
$len = strlen($str);
$out = strtolower($str[0]);
for ($i=1; $i<$len; $i++) {
if(ord($str[$i]) >= ord('A') && (ord($str[$i]) <= ord('Z'))) {
$out .= '_'.strtolower($str[$i]);
}else{
$out .= $str[$i];
}
}
return $out;
}
利用正则来实现
/**
* 驼峰式 与 下划线式 转化
* @param string $str 字符串
* @param string $mode 转化方法 hump驼峰|line下划线
*
* @return mixed|null|string|string[]
*/
function pregConvertString($str,$mode='hump'){
if(empty($str)){
return '';
}
switch ($mode){
case 'hump'://下划线转驼峰
$str = preg_replace_callback('/[-_]+([a-z]{1})/',function($matches){
return strtoupper($matches[1]);
},$str);
$str = ucfirst($str);//首字母大写
break;
case 'line'://驼峰转下划线
$str = str_replace("_", "", $str);
$str = preg_replace_callback('/([A-Z]{1})/',function($matches){
return '_'.strtolower($matches[0]);
},$str);
$str = trim($str,'_');
break;
default:
echo 'mode is error!';
}
return $str;
}
输出结果
$str = 'make_by_id';
echo pregConvertString($str,'hump'),"\n";
echo underLineToHump($str),"\n";
$str = 'MakeById';
echo pregConvertString('MakeById','line'),"\n";
echo humpToUnderLine($str),"\n";
/*
MakeById
MakeById
make_by_id
make_by_id
*/