StringHelper.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 22/02/2017
  6. * Time: 15:14
  7. */
  8. namespace app\common\helpers;
  9. use Illuminate\Support\Str;
  10. class StringHelper extends Str
  11. {
  12. /**
  13. * 驼峰字符分隔
  14. * 如: camelCaseToSplit => camel-case-to-split
  15. *
  16. * @param $string
  17. * @param string $split 分隔符默认 -
  18. * @return string
  19. */
  20. public static function camelCaseToSplit($string, $split = '-')
  21. {
  22. return strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', $split, $string));
  23. }
  24. /**
  25. * 分隔符转驼峰
  26. * 如: camel-case-to-split => camelCaseToSplit
  27. *
  28. * @param $string
  29. * @param string $split
  30. * @return mixed
  31. */
  32. public static function splitToCamelCase($string, $split = '-')
  33. {
  34. return preg_replace_callback(
  35. "/(" . $split . "([a-z]))/",
  36. function ($match) {
  37. return strtoupper($match[2]);
  38. },
  39. $string
  40. );
  41. }
  42. }