下劃線式轉化為駝峰字串

2021-09-29 18:47:01 字數 1354 閱讀 5470

下劃線式轉化為駝峰字串

/*** 下劃線轉駝峰 字串轉化函式 _make_by_id_ => makebyid

** @param $str

** @return string $str string 輸出轉化後的字串

*/function underlinetohump($str)else

}return $out;

}駝峰式轉化為下劃線字串

/*** 駝峰轉下劃線 字串函式 makebyid => make_by_id

* @param $str

** @return string

*/function humptounderline($str)

else

}return $out;

}利用正則來實現

/*** 駝峰式 與 下劃線式 轉化

* @param string $str  字串

* @param string $mode 轉化方法 hump駝峰|line下劃線

** @return mixed|null|string|string

*/function pregconvertstring($str,$mode='hump')

switch ($mode))/',function($matches),$str);

$str    = ucfirst($str);//首字母大寫

break;

case 'line'://駝峰轉下劃線

$str = str_replace("_", "", $str);

$str = preg_replace_callback('/([a-z])/',function($matches),$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

*

字串的下劃線命名和駝峰命名轉換

將駝峰式命名的字串轉換為下劃線大寫方式。如果轉換前的駝峰式命名的字串為空,則返回空字串。例如 helloworld hello world param name 轉換前的駝峰式命名的字串 return 轉換後下劃線大寫方式命名的字串 publicstatic string underscorenam...

字串的下劃線命名和駝峰命名轉換

將駝峰式命名的字串轉換為下劃線大寫方式。如果轉換前的駝峰式命名的字串為空,則返回空字串。例如 helloworld hello world param name 轉換前的駝峰式命名的字串 return 轉換後下劃線大寫方式命名的字串 public static string underscorena...

字串的下劃線命名和駝峰命名轉換

12 3456 78910 1112 1314 1516 1718 1920 2122 2324 2526 2728 2930 3132 3334 3536 3738 3940 4142 4344 4546 4748 4950 5152 5354 5556 5758 5960 將駝峰式命名的字串轉換...