1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | PHPSpider [ A PHP Framework For Crawler ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006-2014 https://doc.phpspider.org All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: Seatle Yang <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
|
12
|
|
|
//---------------------------------- |
13
|
|
|
// PHPSpider实用函数集合类文件 |
14
|
|
|
//---------------------------------- |
15
|
|
|
|
16
|
|
|
namespace phpspider\core; |
17
|
|
|
// 引入PATH_DATA |
18
|
|
|
require_once __DIR__ . '/constants.php'; |
19
|
|
|
|
20
|
|
|
class util |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* 文件锁 |
24
|
|
|
* 如果没有锁,就加一把锁并且执行逻辑,然后删除锁 |
25
|
|
|
* if (!util::lock('statistics_offer')) |
26
|
|
|
* { |
27
|
|
|
* util::lock('statistics_offer'); |
28
|
|
|
* ... |
29
|
|
|
* util::unlock('statistics_offer'); |
30
|
|
|
* } |
31
|
|
|
* 否则输出锁存在 |
32
|
|
|
* else |
33
|
|
|
* { |
34
|
|
|
* echo "process has been locked\n"; |
35
|
|
|
* } |
36
|
|
|
* |
37
|
|
|
* @param mixed $lock_name |
38
|
|
|
* @param int $lock_timeout |
39
|
|
|
* @return void |
40
|
|
|
* @author seatle <[email protected]> |
41
|
|
|
* @created time :2016-02-18 14:28 |
42
|
|
|
*/ |
43
|
|
|
public static function lock($lock_name, $lock_timeout = 600) |
44
|
|
|
{ |
45
|
|
|
$lock = util::get_file(PATH_DATA."/lock/{$lock_name}.lock"); |
46
|
|
|
if ($lock) |
47
|
|
|
{ |
48
|
|
|
$time = time() - $lock; |
49
|
|
|
// 还没到10分钟,说明进程还活着 |
50
|
|
|
if ($time < $lock_timeout) |
51
|
|
|
{ |
52
|
|
|
return true; |
|
|
|
|
53
|
|
|
} |
54
|
|
|
unlink(PATH_DATA."/lock/{$lock_name}.lock"); |
55
|
|
|
} |
56
|
|
|
util::put_file(PATH_DATA."/lock/{$lock_name}.lock", time()); |
57
|
|
|
return false; |
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function unlock($lock_name) |
61
|
|
|
{ |
62
|
|
|
unlink(PATH_DATA."/lock/{$lock_name}.lock"); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public static function time2second($time, $is_log = true) |
66
|
|
|
{ |
67
|
|
|
if(is_numeric($time)) |
68
|
|
|
{ |
69
|
|
|
$value = array( |
70
|
|
|
"years" => 0, "days" => 0, "hours" => 0, |
71
|
|
|
"minutes" => 0, "seconds" => 0, |
72
|
|
|
); |
73
|
|
|
if($time >= 31556926) |
74
|
|
|
{ |
75
|
|
|
$value["years"] = floor($time/31556926); |
76
|
|
|
$time = ($time%31556926); |
77
|
|
|
} |
78
|
|
|
if($time >= 86400) |
79
|
|
|
{ |
80
|
|
|
$value["days"] = floor($time/86400); |
81
|
|
|
$time = ($time%86400); |
82
|
|
|
} |
83
|
|
|
if($time >= 3600) |
84
|
|
|
{ |
85
|
|
|
$value["hours"] = floor($time/3600); |
86
|
|
|
$time = ($time%3600); |
87
|
|
|
} |
88
|
|
|
if($time >= 60) |
89
|
|
|
{ |
90
|
|
|
$value["minutes"] = floor($time/60); |
91
|
|
|
$time = ($time%60); |
92
|
|
|
} |
93
|
|
|
$value["seconds"] = floor($time); |
94
|
|
|
//return (array) $value; |
95
|
|
|
//$t = $value["years"] ."y ". $value["days"] ."d ". $value["hours"] ."h ". $value["minutes"] ."m ".$value["seconds"]."s"; |
96
|
|
|
if ($is_log) |
97
|
|
|
{ |
98
|
|
|
$t = $value["days"] ."d ". $value["hours"] ."h ". $value["minutes"] ."m ".$value["seconds"]."s"; |
99
|
|
|
} |
100
|
|
|
else |
101
|
|
|
{ |
102
|
|
|
$t = $value["days"] ." days ". $value["hours"] ." hours ". $value["minutes"] ." minutes"; |
103
|
|
|
} |
104
|
|
|
return $t; |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
else |
108
|
|
|
{ |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function get_days($day_sta, $day_end = true, $range = 86400) |
114
|
|
|
{ |
115
|
|
|
if ($day_end === true) $day_end = date('Y-m-d'); |
116
|
|
|
|
117
|
|
|
return array_map(function ($time) { |
118
|
|
|
return date('Y-m-d', $time); |
119
|
|
|
}, range(strtotime($day_sta), strtotime($day_end), $range)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* 获取文件行数 |
124
|
|
|
* |
125
|
|
|
* @param mixed $filepath |
126
|
|
|
* @return void |
127
|
|
|
* @author seatle <[email protected]> |
128
|
|
|
* @created time :2016-03-31 21:54 |
129
|
|
|
*/ |
130
|
|
|
public static function get_file_line($filepath) |
131
|
|
|
{ |
132
|
|
|
$line = 0 ; |
133
|
|
|
$fp = fopen($filepath , 'r'); |
134
|
|
|
if (!$fp) |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
return 0; |
|
|
|
|
137
|
|
|
} |
138
|
|
|
//获取文件的一行内容,注意:需要php5才支持该函数; |
139
|
|
|
while( stream_get_line($fp,8192,"\n") ){ |
140
|
|
|
$line++; |
141
|
|
|
} |
142
|
|
|
fclose($fp);//关闭文件 |
143
|
|
|
return $line; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* 获得表数 |
148
|
|
|
* |
149
|
|
|
* @param mixed $table_name 表名 |
150
|
|
|
* @param mixed $item_value 唯一索引 |
151
|
|
|
* @param int $table_num 表数量 |
152
|
|
|
* @return void |
153
|
|
|
* @author seatle <[email protected]> |
154
|
|
|
* @created time :2015-10-22 23:25 |
155
|
|
|
*/ |
156
|
|
|
public static function get_table_num($item_value, $table_num = 100) |
157
|
|
|
{ |
158
|
|
|
//sha1:返回一个40字符长度的16进制数字 |
159
|
|
|
$item_value = sha1(strtolower($item_value)); |
160
|
|
|
//base_convert:进制建转换,下面是把16进制转成10进制,方便做除法运算 |
161
|
|
|
//str_pad:把字符串填充为指定的长度,下面是在左边加0,表数量大于100就3位,否则2位 |
162
|
|
|
$step = $table_num > 100 ? 3 : 2; |
163
|
|
|
$item_value = str_pad(base_convert(substr($item_value, -2), 16, 10) % $table_num, $step, "0", STR_PAD_LEFT); |
164
|
|
|
return $item_value; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* 获得表面 |
169
|
|
|
* |
170
|
|
|
* @param mixed $table_name 表名 |
171
|
|
|
* @param mixed $item_value 唯一索引 |
172
|
|
|
* @param int $table_num 表数量 |
173
|
|
|
* @return void |
174
|
|
|
* @author seatle <[email protected]> |
175
|
|
|
* @created time :2015-10-22 23:25 |
176
|
|
|
*/ |
177
|
|
|
public static function get_table_name($table_name, $item_value, $table_num = 100) |
178
|
|
|
{ |
179
|
|
|
//sha1:返回一个40字符长度的16进制数字 |
180
|
|
|
$item_value = sha1(strtolower($item_value)); |
181
|
|
|
//base_convert:进制建转换,下面是把16进制转成10进制,方便做除法运算 |
182
|
|
|
//str_pad:把字符串填充为指定的长度,下面是在左边加0,共3位 |
183
|
|
|
$step = $table_num > 100 ? 3 : 2; |
184
|
|
|
$item_value = str_pad(base_convert(substr($item_value, -2), 16, 10) % $table_num, $step, "0", STR_PAD_LEFT); |
185
|
|
|
return $table_name."_".$item_value; |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// 获得当前使用内存 |
189
|
|
|
public static function memory_get_usage() |
190
|
|
|
{ |
191
|
|
|
$memory = memory_get_usage(); |
192
|
|
|
return self::format_bytes($memory); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// 获得最高使用内存 |
196
|
|
|
public static function memory_get_peak_usage() |
197
|
|
|
{ |
198
|
|
|
$memory = memory_get_peak_usage(); |
199
|
|
|
return self::format_bytes($memory); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// 转换大小单位 |
203
|
|
|
public static function format_bytes($size) |
204
|
|
|
{ |
205
|
|
|
$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); |
206
|
|
|
return @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* 获取数组大小 |
211
|
|
|
* |
212
|
|
|
* @param mixed $arr 数组 |
213
|
|
|
* @return string |
214
|
|
|
*/ |
215
|
|
|
public static function array_size($arr) |
216
|
|
|
{ |
217
|
|
|
ob_start(); |
218
|
|
|
print_r($arr); |
219
|
|
|
$mem = ob_get_contents(); |
220
|
|
|
ob_end_clean(); |
221
|
|
|
$mem = preg_replace("/\n +/", "", $mem); |
222
|
|
|
$mem = strlen($mem); |
223
|
|
|
return self::format_bytes($mem); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* 数字随机数 |
228
|
|
|
* |
229
|
|
|
* @param int $num |
230
|
|
|
* @return void |
231
|
|
|
* @author seatle <[email protected]> |
232
|
|
|
* @created time :2016-09-18 10:17 |
233
|
|
|
*/ |
234
|
|
|
public static function rand_num($num = 7) |
235
|
|
|
{ |
236
|
|
|
$rand = ""; |
237
|
|
|
for ($i = 0; $i < $num; $i ++) |
238
|
|
|
{ |
239
|
|
|
$rand .= mt_rand(0, 9); |
240
|
|
|
} |
241
|
|
|
return $rand; |
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* 字母数字混合随机数 |
246
|
|
|
* |
247
|
|
|
* @param int $num |
248
|
|
|
* @return void |
249
|
|
|
* @author seatle <[email protected]> |
250
|
|
|
* @created time :2016-09-18 10:17 |
251
|
|
|
*/ |
252
|
|
|
public static function rand_str($num = 10) |
253
|
|
|
{ |
254
|
|
|
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; |
255
|
|
|
$string = ""; |
256
|
|
|
for ($i = 0; $i < $num; $i ++) |
257
|
|
|
{ |
258
|
|
|
$string .= substr($chars, rand(0, strlen($chars)), 1); |
259
|
|
|
} |
260
|
|
|
return $string; |
|
|
|
|
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* 汉字转拼音 |
265
|
|
|
* |
266
|
|
|
* @param mixed $str 汉字 |
267
|
|
|
* @param int $ishead |
268
|
|
|
* @param int $isclose |
269
|
|
|
* @static |
270
|
|
|
* @access public |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public static function pinyin($str, $ishead = 0, $isclose = 1) |
274
|
|
|
{ |
275
|
|
|
// $str = iconv("utf-8", "gbk//ignore", $str); |
276
|
|
|
$str = mb_convert_encoding($str, "gbk", "utf-8"); |
277
|
|
|
global $pinyins; |
278
|
|
|
$restr = ''; |
279
|
|
|
$str = trim($str); |
280
|
|
|
$slen = strlen($str); |
281
|
|
|
if ($slen < 2) |
282
|
|
|
{ |
283
|
|
|
return $str; |
284
|
|
|
} |
285
|
|
|
if (count($pinyins) == 0) |
286
|
|
|
{ |
287
|
|
|
$fp = fopen(PATH_DATA . '/pinyin.dat', 'r'); |
288
|
|
|
while (!feof($fp)) |
|
|
|
|
289
|
|
|
{ |
290
|
|
|
$line = trim(fgets($fp)); |
|
|
|
|
291
|
|
|
$pinyins[$line[0] . $line[1]] = substr($line, 3, strlen($line) - 3); |
292
|
|
|
} |
293
|
|
|
fclose($fp); |
|
|
|
|
294
|
|
|
} |
295
|
|
|
for ($i = 0; $i < $slen; $i ++) |
296
|
|
|
{ |
297
|
|
|
if (ord($str[$i]) > 0x80) |
298
|
|
|
{ |
299
|
|
|
$c = $str[$i] . $str[$i + 1]; |
300
|
|
|
$i ++; |
301
|
|
|
if (isset($pinyins[$c])) |
302
|
|
|
{ |
303
|
|
|
if ($ishead == 0) |
304
|
|
|
{ |
305
|
|
|
$restr .= $pinyins[$c]; |
306
|
|
|
} |
307
|
|
|
else |
308
|
|
|
{ |
309
|
|
|
$restr .= $pinyins[$c][0]; |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
else |
313
|
|
|
{ |
314
|
|
|
// $restr .= "_"; |
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
else if (preg_match("/[a-z0-9]/i", $str[$i])) |
318
|
|
|
{ |
319
|
|
|
$restr .= $str[$i]; |
320
|
|
|
} |
321
|
|
|
else |
322
|
|
|
{ |
323
|
|
|
// $restr .= "_"; |
324
|
|
|
} |
325
|
|
|
} |
326
|
|
|
if ($isclose == 0) |
327
|
|
|
{ |
328
|
|
|
unset($pinyins); |
329
|
|
|
} |
330
|
|
|
return $restr; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* 生成字母前缀 |
335
|
|
|
* |
336
|
|
|
* @param mixed $s0 |
337
|
|
|
* @return char |
|
|
|
|
338
|
|
|
* @author seatle <[email protected]> |
339
|
|
|
* @created time :2016-09-18 10:17 |
340
|
|
|
*/ |
341
|
|
|
public static function letter_first($s0) |
342
|
|
|
{ |
343
|
|
|
$firstchar_ord = ord(strtoupper($s0{0})); |
344
|
|
|
if (($firstchar_ord >= 65 and $firstchar_ord <= 91) or ($firstchar_ord >= 48 and $firstchar_ord <= 57)) return $s0{0}; |
345
|
|
|
// $s = iconv("utf-8", "gbk//ignore", $s0); |
346
|
|
|
$s = mb_convert_encoding($s0, "gbk", "utf-8"); |
347
|
|
|
$asc = ord($s{0}) * 256 + ord($s{1}) - 65536; |
348
|
|
|
if ($asc >= -20319 and $asc <= -20284) return "A"; |
|
|
|
|
349
|
|
|
if ($asc >= -20283 and $asc <= -19776) return "B"; |
|
|
|
|
350
|
|
|
if ($asc >= -19775 and $asc <= -19219) return "C"; |
|
|
|
|
351
|
|
|
if ($asc >= -19218 and $asc <= -18711) return "D"; |
|
|
|
|
352
|
|
|
if ($asc >= -18710 and $asc <= -18527) return "E"; |
|
|
|
|
353
|
|
|
if ($asc >= -18526 and $asc <= -18240) return "F"; |
|
|
|
|
354
|
|
|
if ($asc >= -18239 and $asc <= -17923) return "G"; |
|
|
|
|
355
|
|
|
if ($asc >= -17922 and $asc <= -17418) return "H"; |
|
|
|
|
356
|
|
|
if ($asc >= -17417 and $asc <= -16475) return "J"; |
|
|
|
|
357
|
|
|
if ($asc >= -16474 and $asc <= -16213) return "K"; |
|
|
|
|
358
|
|
|
if ($asc >= -16212 and $asc <= -15641) return "L"; |
|
|
|
|
359
|
|
|
if ($asc >= -15640 and $asc <= -15166) return "M"; |
|
|
|
|
360
|
|
|
if ($asc >= -15165 and $asc <= -14923) return "N"; |
|
|
|
|
361
|
|
|
if ($asc >= -14922 and $asc <= -14915) return "O"; |
|
|
|
|
362
|
|
|
if ($asc >= -14914 and $asc <= -14631) return "P"; |
|
|
|
|
363
|
|
|
if ($asc >= -14630 and $asc <= -14150) return "Q"; |
|
|
|
|
364
|
|
|
if ($asc >= -14149 and $asc <= -14091) return "R"; |
|
|
|
|
365
|
|
|
if ($asc >= -14090 and $asc <= -13319) return "S"; |
|
|
|
|
366
|
|
|
if ($asc >= -13318 and $asc <= -12839) return "T"; |
|
|
|
|
367
|
|
|
if ($asc >= -12838 and $asc <= -12557) return "W"; |
|
|
|
|
368
|
|
|
if ($asc >= -12556 and $asc <= -11848) return "X"; |
|
|
|
|
369
|
|
|
if ($asc >= -11847 and $asc <= -11056) return "Y"; |
|
|
|
|
370
|
|
|
if ($asc >= -11055 and $asc <= -10247) return "Z"; |
|
|
|
|
371
|
|
|
return 0; // null |
|
|
|
|
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* 获得某天前的时间戳 |
376
|
|
|
* |
377
|
|
|
* @param mixed $day |
378
|
|
|
* @return void |
379
|
|
|
* @author seatle <[email protected]> |
380
|
|
|
* @created time :2016-09-18 10:17 |
381
|
|
|
*/ |
382
|
|
|
public static function getxtime($day) |
383
|
|
|
{ |
384
|
|
|
$day = intval($day); |
385
|
|
|
return mktime(23, 59, 59, date("m"), date("d") - $day, date("y")); |
|
|
|
|
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* 读文件 |
390
|
|
|
*/ |
391
|
|
|
public static function get_file($url, $timeout = 10) |
392
|
|
|
{ |
393
|
|
|
if (function_exists('curl_init')) |
394
|
|
|
{ |
395
|
|
|
$ch = curl_init(); |
396
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
|
|
|
|
397
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
398
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
399
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
400
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); |
401
|
|
|
$content = curl_exec($ch); |
|
|
|
|
402
|
|
|
curl_close($ch); |
|
|
|
|
403
|
|
|
if ($content) return $content; |
404
|
|
|
} |
405
|
|
|
$ctx = stream_context_create(array('http' => array('timeout' => $timeout))); |
406
|
|
|
$content = @file_get_contents($url, 0, $ctx); |
407
|
|
|
if ($content) return $content; |
408
|
|
|
return false; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* 写文件,如果文件目录不存在,则递归生成 |
413
|
|
|
*/ |
414
|
|
|
public static function put_file($file, $content, $flag = 0) |
415
|
|
|
{ |
416
|
|
|
$pathinfo = pathinfo($file); |
417
|
|
|
if (!empty($pathinfo['dirname'])) |
418
|
|
|
{ |
419
|
|
|
if (file_exists($pathinfo['dirname']) === false) |
420
|
|
|
{ |
421
|
|
|
if (@mkdir($pathinfo['dirname'], 0777, true) === false) |
422
|
|
|
{ |
423
|
|
|
return false; |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
} |
427
|
|
|
if ($flag === FILE_APPEND) |
428
|
|
|
{ |
429
|
|
|
// 多个php-fpm写一个文件的时候容易丢失,要加锁 |
430
|
|
|
//return @file_put_contents($file, $content, FILE_APPEND|LOCK_EX); |
431
|
|
|
return @file_put_contents($file, $content, FILE_APPEND); |
432
|
|
|
} |
433
|
|
|
else |
434
|
|
|
{ |
435
|
|
|
return @file_put_contents($file, $content, LOCK_EX); |
436
|
|
|
} |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
/** |
440
|
|
|
* 检查路径是否存在,不存在则递归生成路径 |
441
|
|
|
* |
442
|
|
|
* @param mixed $path 路径 |
443
|
|
|
* @static |
444
|
|
|
* @access public |
445
|
|
|
* @return bool or string |
446
|
|
|
*/ |
447
|
|
|
public static function path_exists($path) |
448
|
|
|
{ |
449
|
|
|
$pathinfo = pathinfo($path . '/tmp.txt'); |
450
|
|
|
if (!empty($pathinfo['dirname'])) |
451
|
|
|
{ |
452
|
|
|
if (file_exists($pathinfo['dirname']) === false) |
453
|
|
|
{ |
454
|
|
|
if (mkdir($pathinfo['dirname'], 0777, true) === false) |
455
|
|
|
{ |
456
|
|
|
return false; |
457
|
|
|
} |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
return $path; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* 递归删除目录 |
465
|
|
|
* |
466
|
|
|
* @param mixed $dir |
467
|
|
|
* @return void |
468
|
|
|
* @author seatle <[email protected]> |
469
|
|
|
* @created time :2016-09-18 10:17 |
470
|
|
|
*/ |
471
|
|
|
public static function deldir($dir) |
472
|
|
|
{ |
473
|
|
|
//先删除目录下的文件: |
474
|
|
|
$dh = opendir($dir); |
475
|
|
|
while ($file = readdir($dh)) |
|
|
|
|
476
|
|
|
{ |
477
|
|
|
if($file!="." && $file!="..") |
478
|
|
|
{ |
479
|
|
|
$fullpath = $dir."/".$file; |
480
|
|
|
if(!is_dir($fullpath)) |
481
|
|
|
{ |
482
|
|
|
unlink($fullpath); |
483
|
|
|
} |
484
|
|
|
else |
485
|
|
|
{ |
486
|
|
|
self::deldir($fullpath); |
487
|
|
|
} |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
|
491
|
|
|
closedir($dh); |
|
|
|
|
492
|
|
|
//删除当前文件夹: |
493
|
|
|
if(rmdir($dir)) |
494
|
|
|
{ |
495
|
|
|
return true; |
|
|
|
|
496
|
|
|
} |
497
|
|
|
else |
498
|
|
|
{ |
499
|
|
|
return false; |
|
|
|
|
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
/** |
504
|
|
|
* 递归修改目录权限 |
505
|
|
|
* |
506
|
|
|
* @param mixed $path 目录 |
507
|
|
|
* @param mixed $filemode 权限 |
508
|
|
|
* @return bool |
509
|
|
|
*/ |
510
|
|
|
public static function chmodr($path, $filemode) |
511
|
|
|
{ |
512
|
|
|
if (!is_dir($path)) |
513
|
|
|
{ |
514
|
|
|
return @chmod($path, $filemode); |
515
|
|
|
} |
516
|
|
|
|
517
|
|
|
$dh = opendir($path); |
518
|
|
|
while (($file = readdir($dh)) !== false) |
|
|
|
|
519
|
|
|
{ |
520
|
|
|
if ($file != '.' && $file != '..') |
521
|
|
|
{ |
522
|
|
|
$fullpath = $path . '/' . $file; |
523
|
|
|
if (is_link($fullpath)) |
524
|
|
|
{ |
525
|
|
|
return FALSE; |
526
|
|
|
} |
527
|
|
|
elseif (!is_dir($fullpath) && !@chmod($fullpath, $filemode)) |
528
|
|
|
{ |
529
|
|
|
return FALSE; |
530
|
|
|
} |
531
|
|
|
elseif (!self::chmodr($fullpath, $filemode)) |
532
|
|
|
{ |
533
|
|
|
return FALSE; |
534
|
|
|
} |
535
|
|
|
} |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
closedir($dh); |
|
|
|
|
539
|
|
|
|
540
|
|
|
if (@chmod($path, $filemode)) |
541
|
|
|
{ |
542
|
|
|
return TRUE; |
543
|
|
|
} |
544
|
|
|
else |
545
|
|
|
{ |
546
|
|
|
return FALSE; |
547
|
|
|
} |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* 数组格式化为CSV |
552
|
|
|
* |
553
|
|
|
* @param mixed $data |
554
|
|
|
* @return void |
555
|
|
|
* @author seatle <[email protected]> |
556
|
|
|
* @created time :2016-07-29 11:32 |
557
|
|
|
*/ |
558
|
|
|
public static function format_csv($data) |
559
|
|
|
{ |
560
|
|
|
foreach ($data as $k=>$v) |
561
|
|
|
{ |
562
|
|
|
$v = str_replace(",", "", $v); |
563
|
|
|
$v = str_replace(",", "", $v); |
564
|
|
|
$data[$k] = $v; |
565
|
|
|
} |
566
|
|
|
return implode(",", $data); |
|
|
|
|
567
|
|
|
} |
568
|
|
|
|
569
|
|
|
/** |
570
|
|
|
* 判断是否为utf8字符串 |
571
|
|
|
* @parem $str |
572
|
|
|
* @return bool |
573
|
|
|
*/ |
574
|
|
|
public static function is_utf8($str) |
575
|
|
|
{ |
576
|
|
|
if ($str === mb_convert_encoding(mb_convert_encoding($str, "UTF-32", "UTF-8"), "UTF-8", "UTF-32")) |
577
|
|
|
{ |
578
|
|
|
return true; |
579
|
|
|
} |
580
|
|
|
else |
581
|
|
|
{ |
582
|
|
|
return false; |
583
|
|
|
} |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* 获取文件编码 |
588
|
|
|
* @param $string |
589
|
|
|
* @return string |
590
|
|
|
*/ |
591
|
|
|
public static function get_encoding($string) |
592
|
|
|
{ |
593
|
|
|
$encoding = mb_detect_encoding($string, array('UTF-8', 'GBK', 'GB2312', 'LATIN1', 'ASCII', 'BIG5')); |
594
|
|
|
return strtolower($encoding); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* 转换数组值的编码格式 |
599
|
|
|
* @param array $arr |
600
|
|
|
* @param string $toEncoding |
601
|
|
|
* @param string $fromEncoding |
602
|
|
|
* @return array |
603
|
|
|
*/ |
604
|
|
|
public static function array_iconv($arr, $from_encoding, $to_encoding) |
605
|
|
|
{ |
606
|
|
|
eval('$arr = '.iconv($from_encoding, $to_encoding.'//IGNORE', var_export($arr,TRUE)).';'); |
|
|
|
|
607
|
|
|
return $arr; |
608
|
|
|
} |
609
|
|
|
|
610
|
|
|
/** |
611
|
|
|
* 从普通时间返回Linux时间截(strtotime中文处理版) |
612
|
|
|
* @parem string $dtime |
613
|
|
|
* @return int |
614
|
|
|
*/ |
615
|
|
|
public static function cn_strtotime($dtime) |
616
|
|
|
{ |
617
|
|
|
if (!preg_match("/[^0-9]/", $dtime)) |
618
|
|
|
{ |
619
|
|
|
return $dtime; |
620
|
|
|
} |
621
|
|
|
$dtime = trim($dtime); |
622
|
|
|
$dt = Array(1970, 1, 1, 0, 0, 0); |
623
|
|
|
$dtime = preg_replace("/[\r\n\t]|日|秒/", " ", $dtime); |
624
|
|
|
$dtime = str_replace("年", "-", $dtime); |
625
|
|
|
$dtime = str_replace("月", "-", $dtime); |
626
|
|
|
$dtime = str_replace("时", ":", $dtime); |
627
|
|
|
$dtime = str_replace("分", ":", $dtime); |
628
|
|
|
$dtime = trim(preg_replace("/[ ]{1,}/", " ", $dtime)); |
629
|
|
|
$ds = explode(" ", $dtime); |
630
|
|
|
$ymd = explode("-", $ds[0]); |
631
|
|
|
if (!isset($ymd[1])) |
632
|
|
|
{ |
633
|
|
|
$ymd = explode(".", $ds[0]); |
634
|
|
|
} |
635
|
|
|
if (isset($ymd[0])) |
636
|
|
|
{ |
637
|
|
|
$dt[0] = $ymd[0]; |
638
|
|
|
} |
639
|
|
|
if (isset($ymd[1])) $dt[1] = $ymd[1]; |
640
|
|
|
if (isset($ymd[2])) $dt[2] = $ymd[2]; |
641
|
|
|
if (strlen($dt[0]) == 2) $dt[0] = '20' . $dt[0]; |
642
|
|
|
if (isset($ds[1])) |
643
|
|
|
{ |
644
|
|
|
$hms = explode(":", $ds[1]); |
645
|
|
|
if (isset($hms[0])) $dt[3] = $hms[0]; |
646
|
|
|
if (isset($hms[1])) $dt[4] = $hms[1]; |
647
|
|
|
if (isset($hms[2])) $dt[5] = $hms[2]; |
648
|
|
|
} |
649
|
|
|
foreach ($dt as $k => $v) |
650
|
|
|
{ |
651
|
|
|
$v = preg_replace("/^0{1,}/", '', trim($v)); |
652
|
|
|
if ($v == '') |
653
|
|
|
{ |
654
|
|
|
$dt[$k] = 0; |
655
|
|
|
} |
656
|
|
|
} |
657
|
|
|
$mt = mktime($dt[3], $dt[4], $dt[5], $dt[1], $dt[2], $dt[0]); |
658
|
|
|
if (!empty($mt)) |
659
|
|
|
{ |
660
|
|
|
return $mt; |
661
|
|
|
} |
662
|
|
|
else |
663
|
|
|
{ |
664
|
|
|
return strtotime($dtime); |
665
|
|
|
} |
666
|
|
|
} |
667
|
|
|
|
668
|
|
|
public static function cn_substr($string, $length = 80, $etc = '...', $count_words = true) |
669
|
|
|
{ |
670
|
|
|
mb_internal_encoding("UTF-8"); |
671
|
|
|
if ($length == 0) return ''; |
672
|
|
|
if (strlen($string) <= $length) return $string; |
673
|
|
|
preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $string, $info); |
674
|
|
|
if ($count_words) |
675
|
|
|
{ |
676
|
|
|
$j = 0; |
677
|
|
|
$wordscut = ""; |
678
|
|
|
for ($i = 0; $i < count($info[0]); $i ++) |
|
|
|
|
679
|
|
|
{ |
680
|
|
|
$wordscut .= $info[0][$i]; |
681
|
|
|
if (ord($info[0][$i]) >= 128) |
682
|
|
|
{ |
683
|
|
|
$j = $j + 2; |
684
|
|
|
} |
685
|
|
|
else |
686
|
|
|
{ |
687
|
|
|
$j = $j + 1; |
688
|
|
|
} |
689
|
|
|
if ($j >= $length) |
690
|
|
|
{ |
691
|
|
|
return $wordscut . $etc; |
692
|
|
|
} |
693
|
|
|
} |
694
|
|
|
return join('', $info[0]); |
695
|
|
|
} |
696
|
|
|
return join("", array_slice($info[0], 0, $length)) . $etc; |
697
|
|
|
} |
698
|
|
|
|
699
|
|
|
/** |
700
|
|
|
* 获取文件后缀名 |
701
|
|
|
* |
702
|
|
|
* @param mixed $file_name 文件名 |
703
|
|
|
* @static |
704
|
|
|
* |
705
|
|
|
* @access public |
706
|
|
|
* @return string |
707
|
|
|
*/ |
708
|
|
|
public static function get_extension($file_name) |
709
|
|
|
{ |
710
|
|
|
$ext = explode('.', $file_name); |
711
|
|
|
$ext = array_pop($ext); |
712
|
|
|
return strtolower($ext); |
713
|
|
|
} |
714
|
|
|
|
715
|
|
|
// 获取 Url 跳转后的真实地址 |
716
|
|
|
public static function getrealurl($url) |
717
|
|
|
{ |
718
|
|
|
if (empty($url)) |
719
|
|
|
{ |
720
|
|
|
return $url; |
721
|
|
|
} |
722
|
|
|
$header = get_headers($url, 1); |
723
|
|
|
if (empty($header[0]) || empty($header[1])) |
724
|
|
|
{ |
725
|
|
|
return $url; |
726
|
|
|
} |
727
|
|
|
if (strpos($header[0], '301') || strpos($header[0], '302')) |
728
|
|
|
{ |
729
|
|
|
if (empty($header['Location'])) |
730
|
|
|
{ |
731
|
|
|
return $url; |
732
|
|
|
} |
733
|
|
|
if (is_array($header['Location'])) |
734
|
|
|
{ |
735
|
|
|
return $header['Location'][count($header['Location']) - 1]; |
736
|
|
|
} |
737
|
|
|
else |
738
|
|
|
{ |
739
|
|
|
return $header['Location']; |
740
|
|
|
} |
741
|
|
|
} |
742
|
|
|
else |
743
|
|
|
{ |
744
|
|
|
return $url; |
745
|
|
|
} |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
// 解压服务器用 Content-Encoding:gzip 压缩过的数据 |
749
|
|
|
public static function gzdecode($data) |
750
|
|
|
{ |
751
|
|
|
$flags = ord(substr($data, 3, 1)); |
752
|
|
|
$headerlen = 10; |
753
|
|
|
$extralen = 0; |
|
|
|
|
754
|
|
|
$filenamelen = 0; |
|
|
|
|
755
|
|
|
if ($flags & 4) |
756
|
|
|
{ |
757
|
|
|
$extralen = unpack('v', substr($data, 10, 2)); |
758
|
|
|
$extralen = $extralen[1]; |
759
|
|
|
$headerlen += 2 + $extralen; |
760
|
|
|
} |
761
|
|
|
if ($flags & 8) // Filename |
762
|
|
|
$headerlen = strpos($data, chr(0), $headerlen) + 1; |
763
|
|
|
if ($flags & 16) // Comment |
764
|
|
|
$headerlen = strpos($data, chr(0), $headerlen) + 1; |
765
|
|
|
if ($flags & 2) // CRC at end of file |
766
|
|
|
$headerlen += 2; |
767
|
|
|
$unpacked = @gzinflate(substr($data, $headerlen)); |
768
|
|
|
if ($unpacked === FALSE) $unpacked = $data; |
769
|
|
|
return $unpacked; |
770
|
|
|
} |
771
|
|
|
|
772
|
|
|
/** |
773
|
|
|
* 数字金额转换为中文 |
774
|
|
|
* @param string|integer|float $num 目标数字 |
775
|
|
|
* @param boolean $sim 使用小写(默认) |
776
|
|
|
* @return string |
777
|
|
|
*/ |
778
|
|
|
public static function number2chinese($num, $sim = FALSE) |
779
|
|
|
{ |
780
|
|
|
if (!is_numeric($num)) return '含有非数字非小数点字符!'; |
781
|
|
|
$char = $sim ? array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九') : array('零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'); |
782
|
|
|
$unit = $sim ? array('', '十', '百', '千', '', '万', '亿', '兆') : array('', '拾', '佰', '仟', '', '萬', '億', '兆'); |
783
|
|
|
$retval = ''; |
784
|
|
|
|
785
|
|
|
$num = sprintf("%01.2f", $num); |
786
|
|
|
|
787
|
|
|
list ($num, $dec) = explode('.', $num); |
788
|
|
|
|
789
|
|
|
// 小数部分 |
790
|
|
|
if ($dec['0'] > 0) |
791
|
|
|
{ |
792
|
|
|
$retval .= "{$char[$dec['0']]}角"; |
793
|
|
|
} |
794
|
|
|
if ($dec['1'] > 0) |
795
|
|
|
{ |
796
|
|
|
$retval .= "{$char[$dec['1']]}分"; |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
// 整数部分 |
800
|
|
|
if ($num > 0) |
801
|
|
|
{ |
802
|
|
|
$retval = "元" . $retval; |
803
|
|
|
$f = 1; |
804
|
|
|
$str = strrev(intval($num)); |
805
|
|
|
for ($i = 0, $c = strlen($str); $i < $c; $i ++) |
806
|
|
|
{ |
807
|
|
|
if ($str[$i] > 0) |
808
|
|
|
{ |
809
|
|
|
$f = 0; |
810
|
|
|
} |
811
|
|
|
if ($f == 1 && $str[$i] == 0) |
812
|
|
|
{ |
813
|
|
|
$out[$i] = ""; |
814
|
|
|
} |
815
|
|
|
else |
816
|
|
|
{ |
817
|
|
|
$out[$i] = $char[$str[$i]]; |
818
|
|
|
} |
819
|
|
|
$out[$i] .= $str[$i] != '0' ? $unit[$i % 4] : ''; |
|
|
|
|
820
|
|
|
if ($i > 1 and $str[$i] + $str[$i - 1] == 0) |
821
|
|
|
{ |
822
|
|
|
$out[$i] = ''; |
823
|
|
|
} |
824
|
|
|
if ($i % 4 == 0) |
825
|
|
|
{ |
826
|
|
|
$out[$i] .= $unit[4 + floor($i / 4)]; |
827
|
|
|
} |
828
|
|
|
} |
829
|
|
|
$retval = join('', array_reverse($out)) . $retval; |
830
|
|
|
} |
831
|
|
|
return $retval; |
832
|
|
|
} |
833
|
|
|
|
834
|
|
|
public static function colorize($str, $status = "info") |
835
|
|
|
{ |
836
|
|
|
$out = ""; |
837
|
|
|
switch ($status) |
838
|
|
|
{ |
839
|
|
|
case 'succ': |
840
|
|
|
$out = "\033[32m"; // Blue |
841
|
|
|
break; |
842
|
|
|
case "error": |
843
|
|
|
$out = "\033[31m"; // Red |
844
|
|
|
break; |
845
|
|
|
case "warn": |
846
|
|
|
$out = "\033[33m"; // Yellow |
847
|
|
|
break; |
848
|
|
|
case "note": |
849
|
|
|
$out = "\033[34m"; // Green |
850
|
|
|
break; |
851
|
|
|
case "debug": |
852
|
|
|
$out = "\033[36m"; // Green |
853
|
|
|
break; |
854
|
|
|
default: |
855
|
|
|
$out = "\033[0m"; // info |
856
|
|
|
break; |
857
|
|
|
} |
858
|
|
|
return $out.$str."\033[0m"; |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
public static function node_to_array($dom, $node) |
862
|
|
|
{ |
863
|
|
|
if(!is_a( $dom, 'DOMDocument' ) || !is_a( $node, 'DOMNode' )) |
864
|
|
|
{ |
865
|
|
|
return false; |
866
|
|
|
} |
867
|
|
|
|
868
|
|
|
$array = array(); |
869
|
|
|
// Discard empty nodes |
870
|
|
|
$localName = trim( $node->localName ); |
871
|
|
|
if( empty($localName)) |
872
|
|
|
{ |
873
|
|
|
return false; |
874
|
|
|
} |
875
|
|
|
if( XML_TEXT_NODE == $node->nodeType ) |
876
|
|
|
{ |
877
|
|
|
return $node->nodeValue; |
878
|
|
|
} |
879
|
|
|
foreach ($node->attributes as $attr) |
880
|
|
|
{ |
881
|
|
|
$array['@'.$attr->localName] = $attr->nodeValue; |
882
|
|
|
} |
883
|
|
|
foreach ($node->childNodes as $childNode) |
884
|
|
|
{ |
885
|
|
|
if ( (isset($childNode->childNodes->length) && 1 == $childNode->childNodes->length) && |
886
|
|
|
XML_TEXT_NODE == $childNode->firstChild->nodeType ) |
887
|
|
|
{ |
888
|
|
|
$array[$childNode->localName] = $childNode->nodeValue; |
889
|
|
|
} |
890
|
|
|
else |
891
|
|
|
{ |
892
|
|
|
if( false !== ($a = self::node_to_array( $dom, $childNode))) |
893
|
|
|
{ |
894
|
|
|
$array[$childNode->localName] = $a; |
895
|
|
|
} |
896
|
|
|
} |
897
|
|
|
} |
898
|
|
|
return $array; |
899
|
|
|
} |
900
|
|
|
|
901
|
|
|
public static function is_win() |
902
|
|
|
{ |
903
|
|
|
return strtoupper(substr(PHP_OS,0,3))==="WIN"; |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
/** |
907
|
|
|
* 和 http_build_query 相反,分解出参数 |
908
|
|
|
* |
909
|
|
|
* @return void |
910
|
|
|
* @author seatle <[email protected]> |
911
|
|
|
* @created time :2016-05-16 17:29 |
912
|
|
|
*/ |
913
|
|
|
public static function http_split_query($query, $is_query = false) |
914
|
|
|
{ |
915
|
|
|
if (!$is_query) |
916
|
|
|
{ |
917
|
|
|
$parse_arr = parse_url($query); |
918
|
|
|
if (empty($parse_arr['query'])) |
919
|
|
|
{ |
920
|
|
|
return array(); |
|
|
|
|
921
|
|
|
} |
922
|
|
|
$query = $parse_arr['query']; |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
$query_arr = explode("&", $query); |
926
|
|
|
$params = array(); |
927
|
|
|
foreach ($query_arr as $val) |
928
|
|
|
{ |
929
|
|
|
$arr = explode("=", $val); |
930
|
|
|
$params[$arr[0]] = $arr[1]; |
931
|
|
|
} |
932
|
|
|
return $params; |
|
|
|
|
933
|
|
|
} |
934
|
|
|
} |
935
|
|
|
|
936
|
|
|
|
937
|
|
|
|