helpers.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. const MB = 1048576;
  3. const GB = 1073741824;
  4. const Minute = 60;
  5. const Hour = 3600;
  6. const Day = 86400;
  7. const Mbps = 125000;
  8. // base64加密(处理URL)
  9. if (! function_exists('base64url_encode')) {
  10. function base64url_encode(string $data): string
  11. {
  12. return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($data));
  13. }
  14. }
  15. // base64解密(处理URL)
  16. if (! function_exists('base64url_decode')) {
  17. function base64url_decode(string $data): false|string
  18. {
  19. return base64_decode(str_replace(['-', '_'], ['+', '/'], $data));
  20. }
  21. }
  22. // 根据流量值自动转换单位输出
  23. if (! function_exists('formatBytes')) {
  24. function formatBytes($bytes, int $precision = 2): string
  25. {
  26. $units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
  27. $bytes = max($bytes, 0);
  28. $power = floor(($bytes ? log($bytes) : 0) / log(1024));
  29. $power = min($power, count($units) - 1);
  30. $bytes /= 1024 ** $power;
  31. return round($bytes, $precision).' '.$units[$power];
  32. }
  33. }
  34. // 秒转时间
  35. if (! function_exists('formatTime')) {
  36. function formatTime(int $seconds): string
  37. {
  38. $output = '';
  39. $units = [
  40. trans('validation.attributes.day') => 86400,
  41. trans('validation.attributes.hour') => 3600,
  42. trans('validation.attributes.minute') => 60,
  43. trans('validation.attributes.second') => 1,
  44. ];
  45. foreach ($units as $unit => $value) {
  46. if ($seconds >= $value) {
  47. $count = floor($seconds / $value);
  48. $output .= $count.$unit;
  49. $seconds %= $value;
  50. }
  51. }
  52. return $output;
  53. }
  54. }
  55. // 获取系统设置
  56. if (! function_exists('sysConfig')) {
  57. function sysConfig(string $key = '', string $default = ''): array|string
  58. {
  59. return $key ? config("settings.$key", $default) : config('settings');
  60. }
  61. }
  62. // Array values and indexes clean
  63. if (! function_exists('array_clean')) {
  64. function array_clean(array &$array): array
  65. {
  66. foreach ($array as $key => &$value) {
  67. if (is_array($value)) {
  68. $value = array_clean($value);
  69. }
  70. if (empty($value)) {
  71. unset($array[$key]);
  72. }
  73. }
  74. return $array;
  75. }
  76. }
  77. // string url safe sanitize
  78. if (! function_exists('string_urlsafe')) {
  79. function string_urlsafe($string, $force_lowercase = true, $anal = false): string
  80. {
  81. $strip = [
  82. '~', '`', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '=', '+', '[', '{', ']', '}', '\\', '|', ';', ':', '"', "'", '&#8216;', '&#8217;', '&#8220;',
  83. '&#8221;', '&#8211;', '&#8212;', '—', '–', ',', '<', '.', '>', '/', '?',
  84. ];
  85. $clean = trim(str_replace($strip, '_', strip_tags($string)));
  86. $clean = preg_replace('/\s+/', '-', $clean);
  87. $clean = ($anal) ? preg_replace('/[^a-zA-Z0-9]/', '', $clean) : $clean;
  88. if ($force_lowercase) {
  89. if (function_exists('mb_strtolower')) {
  90. $clean = mb_strtolower($clean, 'UTF-8');
  91. } else {
  92. $clean = strtolower($clean);
  93. }
  94. }
  95. return $clean;
  96. }
  97. }