CommonTool.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\admin\service\tool;
  3. class CommonTool
  4. {
  5. /**
  6. * 下划线转驼峰
  7. * @param $str
  8. * @return null|string|string[]
  9. */
  10. public static function lineToHump($str)
  11. {
  12. $str = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
  13. return strtoupper($matches[2]);
  14. }, $str);
  15. return $str;
  16. }
  17. /**
  18. * 驼峰转下划线
  19. * @param $str
  20. * @return null|string|string[]
  21. */
  22. public static function humpToLine($str)
  23. {
  24. $str = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
  25. return '_' . strtolower($matches[0]);
  26. }, $str);
  27. return $str;
  28. }
  29. /**
  30. * 获取真实IP
  31. * @return mixed
  32. */
  33. public static function getRealIp()
  34. {
  35. $ip = $_SERVER['REMOTE_ADDR'];
  36. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
  37. foreach ($matches[0] as $xip) {
  38. if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
  39. $ip = $xip;
  40. break;
  41. }
  42. }
  43. }elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
  44. $ip = $_SERVER['HTTP_CLIENT_IP'];
  45. }elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
  46. $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
  47. }elseif (isset($_SERVER['HTTP_X_REAL_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_IP'])) {
  48. $ip = $_SERVER['HTTP_X_REAL_IP'];
  49. }
  50. return $ip;
  51. }
  52. /**
  53. * 读取文件夹下的所有文件
  54. * @param $path
  55. * @param $basePath
  56. * @return array|mixed
  57. */
  58. public static function readDirAllFiles($path, $basePath = '')
  59. {
  60. list($list, $temp_list) = [[], scandir($path)];
  61. empty($basePath) && $basePath = $path;
  62. foreach ($temp_list as $file) {
  63. if ($file != ".." && $file != ".") {
  64. if (is_dir($path . DIRECTORY_SEPARATOR . $file)) {
  65. $childFiles = self::readDirAllFiles($path . DIRECTORY_SEPARATOR . $file, $basePath);
  66. $list = array_merge($childFiles, $list);
  67. }else {
  68. $filePath = $path . DIRECTORY_SEPARATOR . $file;
  69. $fileName = str_replace($basePath . DIRECTORY_SEPARATOR, '', $filePath);
  70. $list[$fileName] = $filePath;
  71. }
  72. }
  73. }
  74. return $list;
  75. }
  76. /**
  77. * 模板值替换
  78. * @param $string
  79. * @param $array
  80. * @return mixed
  81. */
  82. public static function replaceTemplate($string, $array)
  83. {
  84. foreach ($array as $key => $val) {
  85. if (is_null($val)) $val = '';
  86. $string = str_replace("{{" . $key . "}}", $val, $string);
  87. }
  88. return $string;
  89. }
  90. public static function replaceArrayString(?string $arrayString): string
  91. {
  92. $arrayString = str_replace('array (', '[', $arrayString);
  93. $arrayString = str_replace(')', ']', $arrayString);
  94. $arrayString = str_replace('=>
  95. [', '=> [', $arrayString);
  96. return $arrayString;
  97. }
  98. }