-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTools.php
executable file
·370 lines (339 loc) · 10.1 KB
/
Tools.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
namespace Helper;
class Tools
{
/**
* 导出csv文件到本地
* @param string $filename
* @param string $data
*/
public static function export_csv($filename, $data)
{
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header("Content-Disposition:attachment;filename=" . $filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $data;
}
/**
* 获取gz文件扩展名
* @return string
*/
public static function getJsExt()
{
return strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false ? 'js' : 'js.gz';
}
/**
* 计算百分比
* @param float $all 被除数
* @param float $part 除数
* @return string
*/
public static function calculatedProportion($part, $all)
{
if ($all > 0) {
return round( $part/$all * 100, 2) . '%';
} else {
return "0%";
}
}
public static function getFileLine($file_name, $line_star, $line_end, $isMissHeader = 1)
{
$n = 0;
$handle = fopen($file_name,"r");
$header = [];
$data =[];
if ($handle) {
//取csv文件的头部
if ($isMissHeader) $header = fgetcsv($handle);
while (!feof($handle)) {
++$n;
if (false !== ($out = fgetcsv($handle)) && $n >= $line_star) {
$data[] = $out;
}
if ($n == $line_end) break;
}
fclose($handle);
}
return ['header'=>$header, 'body'=>$data];
}
/**
* 获取汉字首字母
* @param $s0
* @return null|string
*/
public static function getfirstchar($s0)
{
$fchar = ord($s0{0});
if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
$s1 = $s0;
$s2 = $s1;
//$s1 = iconv("UTF-8","gb2312", $s0);
//$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $s0){$s = $s1;}else{$s = $s0;}
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if($asc >= -20319 and $asc <= -20284) return "A";
if($asc >= -20283 and $asc <= -19776) return "B";
if($asc >= -19775 and $asc <= -19219) return "C";
if($asc >= -19218 and $asc <= -18711) return "D";
if($asc >= -18710 and $asc <= -18527) return "E";
if($asc >= -18526 and $asc <= -18240) return "F";
if($asc >= -18239 and $asc <= -17923) return "G";
if($asc >= -17922 and $asc <= -17418) return "I";
if($asc >= -17417 and $asc <= -16475) return "J";
if($asc >= -16474 and $asc <= -16213) return "K";
if($asc >= -16212 and $asc <= -15641) return "L";
if($asc >= -15640 and $asc <= -15166) return "M";
if($asc >= -15165 and $asc <= -14923) return "N";
if($asc >= -14922 and $asc <= -14915) return "O";
if($asc >= -14914 and $asc <= -14631) return "P";
if($asc >= -14630 and $asc <= -14150) return "Q";
if($asc >= -14149 and $asc <= -14091) return "R";
if($asc >= -14090 and $asc <= -13319) return "S";
if($asc >= -13318 and $asc <= -12839) return "T";
if($asc >= -12838 and $asc <= -12557) return "W";
if($asc >= -12556 and $asc <= -11848) return "X";
if($asc >= -11847 and $asc <= -11056) return "Y";
if($asc >= -11055 and $asc <= -10247) return "Z";
return null;
}
/**
* 获取中文字符拼音首字母
* @param string $str
* @return null|string
*/
public static function getFirstCharter($str)
{
if (empty($str)) {
return '';
}
$fChar = ord($str{0});
if ($fChar >= ord('A') && $fChar <= ord('z')) {
return strtoupper($str{0});
}
$s1 = iconv('UTF-8', 'gb2312', $str);
$s2 = iconv('gb2312', 'UTF-8', $s1);
$s = $s2 == $str ? $s1 : $str;
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
if ($asc >= -20319 && $asc <= -20284) {
return 'A';
}
if ($asc >= -20283 && $asc <= -19776) {
return 'B';
}
if ($asc >= -19775 && $asc <= -19219) {
return 'C';
}
if ($asc >= -19218 && $asc <= -18711) {
return 'D';
}
if ($asc >= -18710 && $asc <= -18527) {
return 'E';
}
if ($asc >= -18526 && $asc <= -18240) {
return 'F';
}
if ($asc >= -18239 && $asc <= -17923) {
return 'G';
}
if ($asc >= -17922 && $asc <= -17418) {
return 'H';
}
if ($asc >= -17417 && $asc <= -16475) {
return 'J';
}
if ($asc >= -16474 && $asc <= -16213) {
return 'K';
}
if ($asc >= -16212 && $asc <= -15641) {
return 'L';
}
if ($asc >= -15640 && $asc <= -15166) {
return 'M';
}
if ($asc >= -15165 && $asc <= -14923) {
return 'N';
}
if ($asc >= -14922 && $asc <= -14915) {
return 'O';
}
if ($asc >= -14914 && $asc <= -14631) {
return 'P';
}
if ($asc >= -14630 && $asc <= -14150) {
return 'Q';
}
if ($asc >= -14149 && $asc <= -14091) {
return 'R';
}
if ($asc >= -14090 && $asc <= -13319) {
return 'S';
}
if ($asc >= -13318 && $asc <= -12839) {
return 'T';
}
if ($asc >= -12838 && $asc <= -12557) {
return 'W';
}
if ($asc >= -12556 && $asc <= -11848) {
return 'X';
}
if ($asc >= -11847 && $asc <= -11056) {
return 'Y';
}
if ($asc >= -11055 && $asc <= -10247) {
return 'Z';
}
return null;
}
/**
* 获取汉字首字母简码
* @param $zh
* @return string
*/
public static function getBC($zh){
$ret = "";
$s1 = iconv("UTF-8","gb2312", $zh);
$s2 = iconv("gb2312","UTF-8", $s1);
if($s2 == $zh){$zh = $s1;}
for($i = 0; $i < strlen($zh); $i++){
$s1 = substr($zh,$i,1);
$p = ord($s1);
if($p > 160){
$s2 = substr($zh,$i++,2);
$ret .= self::getfirstchar($s2);
}else{
$ret .= $s1;
}
}
return $ret;
}
/**
* 生成简码
*
* @param string $name 名称,由数字英文字母或汉字组成
*
* @return string
* @author tao.chen
*/
public static function createBC($name)
{
$bc = '';
$len = mb_strlen($name, "UTF8");
for ($i = 0; $i < $len; $i++) {
$charter = mb_substr($name, $i, 1, "UTF8");
if (preg_match("/[a-zA-Z0-9]/", $charter)) {
//匹配到是英文字母或数字,直接输入
$firstCharter = strval(strtoupper($charter));
} else {
//只接受汉字,其他返回null
$firstCharter = self::getFirstCharter($charter);
}
$bc .= $firstCharter;
}
if (empty($bc)) {
$bc = '*';
}
$bc = substr($bc, 0, 10);
return $bc;
}
/**
* 选定数组的某个值做key重组数组
* 适用于二维数组
* @param $arr
* @param $key
* @return array
*/
public static function array_set_key($arr, $key)
{
if (count($arr) == 0) return [];
$formatData = [];
foreach ($arr as $value) {
$formatData[$value[$key]][] = $value;
}
return $formatData;
}
public static function array_set_primary_key($arr, $key)
{
if (count($arr) == 0) return [];
$formatData = [];
foreach ($arr as $value) {
$formatData[$value[$key]] = $value;
}
return $formatData;
}
/**
* 正则过滤 只获取中文字
* @param $chars
* @param string $encoding
* @return string
*/
public static function match_chinese($chars, $encoding = 'utf8')
{
$pattern = ($encoding == 'utf8') ? '/[\x{4e00}-\x{9fa5}]/u' : '/[\x80-\xFF]/';
preg_match_all($pattern, $chars, $result);
$temp = join('', $result[0]);
return $temp;
}
public static function isNotJson($str)
{
return is_null(json_decode($str, true));
}
/**
* 获取当前客户端的IP地址
* @return array|false|string
*/
public static function getIp()
{
if($ip = $_SERVER['REMOTE_ADDR']) return $ip;
if($ip = getenv('HTTP_CLIENT_IP')) return $ip;
if($ip = getenv('HTTP_X_FORWARDED_FOR')) return $ip;
}
public static function getServerIp()
{
return $_SERVER['SERVER_ADDR'];
}
/**
* 求a相对于b的路径
*
* @param $a
* @param $b
* @return string
*/
public static function getRelativePath($a, $b)
{
$path = '';
$arr = explode('/', $a);
$brr = explode('/', $b);
$same = array_intersect_assoc($arr, $brr);//获取两个数组相同的部分
$dir = array_diff_assoc($brr, $same);
for($i = 1; $i <= count($dir)-1; $i++) {
$path .='../';
}
$path .= str_replace(implode('/',$same).'/','', $a);
return $path;
}
/**
* 遍历指定目录下的所有目录和文件
*
* Date: 2019/12/22
* Time: 3:50 PM
*/
public static function get_dir_info($path) {
$handle = opendir($path);//打开目录返回句柄
while(($content = readdir($handle))!== false){
$new_dir = $path . '/' . $content;
if($content == '..' || $content == '.'){
continue;
}
if(is_dir($new_dir)){
echo "目录:".$new_dir . "\n";
self::get_dir_info($new_dir);
}else{
echo "文件:".$path.'/'.$content ."\n";
}
}
}
}