Names.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. namespace inter\utils;
  3. class Names
  4. {
  5. /**
  6. * 下划线转驼峰
  7. * 思路:
  8. * step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
  9. * step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
  10. * @param $uncamelized_words
  11. * @param string $separator
  12. * @return string
  13. */
  14. public static function Camelize($uncamelized_words, $separator = '_')
  15. {
  16. $uncamelized_words = $separator . str_replace($separator, " ", strtolower($uncamelized_words));
  17. return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator);
  18. }
  19. /**
  20. * 驼峰命名转下划线命名
  21. * 思路:
  22. * @param $camelCaps
  23. * @param string $separator
  24. * @return string
  25. */
  26. public static function UnCamelize($camelCaps, $separator = '_')
  27. {
  28. return strtolower(preg_replace('/([a-z])([A-Z])/', "$1" . $separator . "$2", $camelCaps));
  29. }
  30. }