common.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 应用公共文件
  12. if (!function_exists('attr_format')) {
  13. /**
  14. * 格式化属性
  15. * @param $arr
  16. * @return array
  17. */
  18. function attr_format($arr)
  19. {
  20. $data = [];
  21. $res = [];
  22. $count = count($arr);
  23. if ($count > 1) {
  24. for ($i = 0; $i < $count - 1; $i++) {
  25. if ($i == 0) $data = $arr[$i]['detail'];
  26. //替代变量1
  27. foreach ($data as $v) {
  28. foreach ($arr[$i + 1]['detail'] as $g) {
  29. //替代变量2
  30. $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
  31. $tmp[] = $rep2;
  32. if ($i == $count - 2) {
  33. foreach (explode('-$-', $rep2) as $k => $h) {
  34. //替代变量3
  35. $rep3 = explode('_$_', $h);
  36. //替代变量4
  37. $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
  38. }
  39. if ($count == count($rep4['detail'])) {
  40. $res_sort = [];
  41. foreach ($arr as $re) {
  42. $res_sort[$re['value']] = $rep4['detail'][$re['value']];
  43. }
  44. $rep4['detail'] = $res_sort;
  45. $res[] = $rep4;
  46. }
  47. }
  48. }
  49. }
  50. $data = isset($tmp) ? $tmp : [];
  51. }
  52. } else {
  53. $dataArr = [];
  54. foreach ($arr as $k => $v) {
  55. foreach ($v['detail'] as $kk => $vv) {
  56. $dataArr[$kk] = $v['value'] . '_' . $vv;
  57. $res[$kk]['detail'][$v['value']] = $vv;
  58. }
  59. }
  60. $data[] = implode('-', $dataArr);
  61. }
  62. return [$data, $res];
  63. }
  64. }
  65. if (!function_exists('get_month')) {
  66. /**
  67. * 格式化月份
  68. * @param string $time
  69. * @param int $ceil
  70. * @return array
  71. */
  72. function get_month($time = '', $ceil = 0)
  73. {
  74. if (empty($time)) {
  75. $firstday = date("Y-m-01", time());
  76. $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
  77. } else if ($time == 'n') {
  78. if ($ceil != 0)
  79. $season = ceil(date('n') / 3) - $ceil;
  80. else
  81. $season = ceil(date('n') / 3);
  82. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  83. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  84. } else if ($time == 'y') {
  85. $firstday = date('Y-01-01');
  86. $lastday = date('Y-12-31');
  87. } else if ($time == 'h') {
  88. $firstday = date('Y-m-d', strtotime('this week +' . $ceil . ' day')) . ' 00:00:00';
  89. $lastday = date('Y-m-d', strtotime('this week +' . ($ceil + 1) . ' day')) . ' 23:59:59';
  90. }
  91. return array($firstday, $lastday);
  92. }
  93. }
  94. if (!function_exists('clearfile')) {
  95. /**删除目录下所有文件
  96. * @param $path 目录或者文件路径
  97. * @param string $ext
  98. * @return bool
  99. */
  100. function clearfile($path, $ext = '*.log')
  101. {
  102. $files = (array)glob($path . DS . '*');
  103. foreach ($files as $path) {
  104. if (is_dir($path)) {
  105. $matches = glob($path . '/' . $ext);
  106. if (is_array($matches)) {
  107. array_map('unlink', $matches);
  108. }
  109. rmdir($path);
  110. } else {
  111. unlink($path);
  112. }
  113. }
  114. return true;
  115. }
  116. }
  117. if (!function_exists('get_this_class_methods')) {
  118. /**获取当前类方法
  119. * @param $class
  120. * @return array
  121. */
  122. function get_this_class_methods($class, $unarray = [])
  123. {
  124. $arrayall = get_class_methods($class);
  125. if ($parent_class = get_parent_class($class)) {
  126. $arrayparent = get_class_methods($parent_class);
  127. $arraynow = array_diff($arrayall, $arrayparent);//去除父级的
  128. } else {
  129. $arraynow = $arrayall;
  130. }
  131. return array_diff($arraynow, $unarray);//去除无用的
  132. }
  133. }
  134. if (!function_exists('verify_domain')) {
  135. /**
  136. * 验证域名是否合法
  137. * @param string $domain
  138. * @return bool
  139. */
  140. function verify_domain(string $domain): bool
  141. {
  142. $res = "/^(?=^.{3,255}$)(http(s)?:\/\/)?(www\.)?[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+(:\d+)*(\/\w+\.\w+)*$/";
  143. if (preg_match($res, $domain))
  144. return true;
  145. else
  146. return false;
  147. }
  148. }