common.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. $rep1 = [];
  28. foreach ($data as $v) {
  29. foreach ($arr[$i + 1]['detail'] as $g) {
  30. //替代变量2
  31. $rep2 = ($i != 0 ? '' : $arr[$i]['value'] . '_$_') . $v . '-$-' . $arr[$i + 1]['value'] . '_$_' . $g;
  32. $tmp[] = $rep2;
  33. if ($i == $count - 2) {
  34. foreach (explode('-$-', $rep2) as $k => $h) {
  35. //替代变量3
  36. $rep3 = explode('_$_', $h);
  37. //替代变量4
  38. $rep4['detail'][$rep3[0]] = isset($rep3[1]) ? $rep3[1] : '';
  39. }
  40. if($count == count($rep4['detail']))
  41. $res[] = $rep4;
  42. }
  43. }
  44. }
  45. $data = isset($tmp) ? $tmp : [];
  46. }
  47. } else {
  48. $dataArr = [];
  49. foreach ($arr as $k => $v) {
  50. foreach ($v['detail'] as $kk => $vv) {
  51. $dataArr[$kk] = $v['value'] . '_' . $vv;
  52. $res[$kk]['detail'][$v['value']] = $vv;
  53. }
  54. }
  55. $data[] = implode('-', $dataArr);
  56. }
  57. return [$data, $res];
  58. }
  59. }
  60. if (!function_exists('get_month')) {
  61. /**
  62. * 格式化月份
  63. * @param string $time
  64. * @param int $ceil
  65. * @return array
  66. */
  67. function get_month($time = '', $ceil = 0)
  68. {
  69. if (empty($time)) {
  70. $firstday = date("Y-m-01", time());
  71. $lastday = date("Y-m-d", strtotime("$firstday +1 month -1 day"));
  72. } else if ($time == 'n') {
  73. if ($ceil != 0)
  74. $season = ceil(date('n') / 3) - $ceil;
  75. else
  76. $season = ceil(date('n') / 3);
  77. $firstday = date('Y-m-01', mktime(0, 0, 0, ($season - 1) * 3 + 1, 1, date('Y')));
  78. $lastday = date('Y-m-t', mktime(0, 0, 0, $season * 3, 1, date('Y')));
  79. } else if ($time == 'y') {
  80. $firstday = date('Y-01-01');
  81. $lastday = date('Y-12-31');
  82. } else if ($time == 'h') {
  83. $firstday = date('Y-m-d', strtotime('this week +' . $ceil . ' day')) . ' 00:00:00';
  84. $lastday = date('Y-m-d', strtotime('this week +' . ($ceil + 1) . ' day')) . ' 23:59:59';
  85. }
  86. return array($firstday, $lastday);
  87. }
  88. }
  89. if (!function_exists('clearfile')) {
  90. /**删除目录下所有文件
  91. * @param $path 目录或者文件路径
  92. * @param string $ext
  93. * @return bool
  94. */
  95. function clearfile($path, $ext = '*.log')
  96. {
  97. $files = (array)glob($path . DS . '*');
  98. foreach ($files as $path) {
  99. if (is_dir($path)) {
  100. $matches = glob($path . '/' . $ext);
  101. if (is_array($matches)) {
  102. array_map('unlink', $matches);
  103. }
  104. rmdir($path);
  105. } else {
  106. unlink($path);
  107. }
  108. }
  109. return true;
  110. }
  111. }
  112. if (!function_exists('get_this_class_methods')) {
  113. /**获取当前类方法
  114. * @param $class
  115. * @return array
  116. */
  117. function get_this_class_methods($class, $unarray = [])
  118. {
  119. $arrayall = get_class_methods($class);
  120. if ($parent_class = get_parent_class($class)) {
  121. $arrayparent = get_class_methods($parent_class);
  122. $arraynow = array_diff($arrayall, $arrayparent);//去除父级的
  123. } else {
  124. $arraynow = $arrayall;
  125. }
  126. return array_diff($arraynow, $unarray);//去除无用的
  127. }
  128. }
  129. if (!function_exists('verify_domain')) {
  130. /**
  131. * 验证域名是否合法
  132. * @param string $domain
  133. * @return bool
  134. */
  135. function verify_domain(string $domain): bool
  136. {
  137. $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+)*$/";
  138. if (preg_match($res, $domain))
  139. return true;
  140. else
  141. return false;
  142. }
  143. }
  144. if (!function_exists('pr')) {
  145. function pr($var, $int = '')
  146. {
  147. $template = PHP_SAPI !== 'cli' ? '<pre>%s</pre>' : "\n%s\n";
  148. printf($template, print_r($var, true));
  149. if (!empty($int)) {
  150. exit($int);
  151. }
  152. }
  153. }