StringUtil.php 890 B

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace app\utils;
  3. class StringUtil
  4. {
  5. /**
  6. * 下划线转驼峰
  7. *
  8. * @param $words
  9. * @param string $separator
  10. *
  11. * @return string
  12. */
  13. public static function underline2Camel($words, string $separator = '_'): string
  14. {
  15. if (strpos($words, $separator) !== false) {
  16. $words = $separator . str_replace($separator, " ", strtolower($words));
  17. return ltrim(str_replace(" ", "", ucwords($words)), $separator);
  18. } else {
  19. return $words;
  20. }
  21. }
  22. /**
  23. * 驼峰命名转下划线命名
  24. *
  25. * @param $camelCaps
  26. * @param string $separator
  27. *
  28. * @return string
  29. */
  30. public static function camel2Underline($camelCaps, string $separator = '_'): string
  31. {
  32. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  33. }
  34. }