| 12345678910111213141516171819202122232425262728293031323334353637 |
- <?php
- namespace app\utils;
- class StringUtil
- {
- /**
- * 下划线转驼峰
- *
- * @param $words
- * @param string $separator
- *
- * @return string
- */
- public static function underline2Camel($words, string $separator = '_'): string
- {
- if (strpos($words, $separator) !== false) {
- $words = $separator . str_replace($separator, " ", strtolower($words));
- return ltrim(str_replace(" ", "", ucwords($words)), $separator);
- } else {
- return $words;
- }
- }
- /**
- * 驼峰命名转下划线命名
- *
- * @param $camelCaps
- * @param string $separator
- *
- * @return string
- */
- public static function camel2Underline($camelCaps, string $separator = '_'): string
- {
- return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
- }
- }
|