-
Notifications
You must be signed in to change notification settings - Fork 1
/
include.php
61 lines (54 loc) · 1.4 KB
/
include.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
<?php
function getLogPath() {
return __DIR__ . '/logs';
}
function getAllLogs() {
$logs = glob(getLogPath() . '/*.txt');
sort($logs);
return $logs;
}
function get3Logs() {
$logs = getAllLogs();
sort($logs);
$result = [];
$result[] = reset($logs);
$result[] = $logs[floor(count($logs) / 2)];
$result[] = end($logs);
return $result;
}
function getLatestLog() {
$logs = getAllLogs();
rsort($logs);
return $logs[0];
}
function eachLine($file) {
$du = file_get_contents($file);
$fp = fopen($file, 'r');
while ($line = fgetcsv($fp, 1024, "\t")) {
yield $line;
}
}
function getTimeStamp($file) {
$timestamp = basename($file);
if (preg_match('/du_.*?([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2})/', $timestamp, $matches)) {
return DateTime::createFromFormat('Y-m-d_H-i-s', $matches[1])->format('U');
}
return null;
}
/**
* Find position of Nth $occurrence of $needle in $haystack
* Starts from the beginning of the string
**/
function strpos_offset($needle, $haystack, $occurrence) {
// explode the haystack
$arr = explode($needle, $haystack);
// check the needle is not out of bounds
switch( $occurrence ) {
case $occurrence == 0:
return false;
case $occurrence > max(array_keys($arr)):
return false;
default:
return strlen(implode($needle, array_slice($arr, 0, $occurrence)));
}
}