-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommon.php
More file actions
204 lines (184 loc) · 5.42 KB
/
Copy pathCommon.php
File metadata and controls
204 lines (184 loc) · 5.42 KB
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
<?php
namespace ColorGallery;
/**
* Class Common
*
* @package ColorRabbitHome\ColorClass
*/
class Common
{
/**
* Common constructor.
*/
public function __construct()
{
}
/**
* 将字符串替换
*
* @param string $str 待处理的字符串
* @param string $replaceStr 需要替换的字符串
* @param string $result 将第一次结果集传给function
*
* @return string $result
*/
public function diff($str, $replaceStr, $result = '')
{
$start = strspn($str, $replaceStr);
$end = strcspn($str, $replaceStr, $start);
$result .= mb_substr($str, $start, $end);
$str = mb_substr($str, $start + $end);
if (!empty($str)) {
return $this->diff($str, $replaceStr, $result);
}
return $result;
}
/**
* 字符串去重
* @param string $str 需要处理的字符串
* @param string $charset 字符编码
*
* @return bool|string
*/
public function uniqueStr($str, $charset = '')
{
$array = $this->str2Array($str, $charset);
if (!empty($array)) {
$array = array_unique($array);
$res = implode('', $array);
return $res;
}
return false;
}
/**
* 字符串转数组
*
* @param $str
* @param string $charset
*
* @return array
*/
public function str2Array($str, $charset = '')
{
$array = array();
if ($charset == 'UTF-8') {
$strlen = mb_strlen($str);
while ($strlen) {
$array[] = mb_substr($str, 0, 1, $charset);
$str = mb_substr($str, 1, $strlen, $charset);
$strlen = mb_strlen($str);
}
}
if ($charset == '') {
$strlen = strlen($str);
while ($strlen) {
$array[] = substr($str, 0, 1);
$str = substr($str, 1, $strlen);
$strlen = strlen($str);
}
}
return $array;
}
/**
* 按行分隔符读取大文件(100M以上)
*
* @param string $filename 文件名
* @param int $count 读取行数 default 10000
* @param string $sep 行分隔符
* @param int $offset 读取位置 0(正向) | -1(逆向)
* @param int $position 偏移量
*
* @return array
* @throws \ErrorException
*/
public function getFileLines($filename, $count = 10000, $sep = "\n", $offset = 0, $position = 0)
{
$handle = fopen($filename, "a+");
$content = ''; // 最终内容
$pos = $position;
// 正向读取文件
if ($offset === 0) {
if ($position < 0) {
throw new \ErrorException('正向读文件暂不支持postion<0');
}
for ($i = 0; $i < $count;) {
// 读取文件
fseek($handle, $pos, SEEK_SET);
$cContent = fread($handle, strlen($sep)); // 当前读取内容寄存
/*if (strlen($cContent) > 1) {
$content = $content . substr($cContent, 0, 1);// 拼接字符串 备用方案
}*/
$content = substr($content, 0, $pos - $position) . $cContent;// 很准确
$pos++;
if ($sep === $cContent) {
$i++;
}
if (fgets($handle) === false) {
break;
}
}
}
// 逆向读取文件
if ($offset === -1) {
if ($position > 0) {
throw new \ErrorException('反向读文件暂不支持postion>0');
}
for ($i = 0; $i <= $count;) {
// 读取文件
fseek($handle, $pos, SEEK_END);
$cContent = fread($handle, strlen($sep)); // 当前读取内容寄存
if (strlen($cContent) > 1) {
$content = substr($cContent, -1, 1) . $content;// 拼接字符串 备用方案
} else {
$content = $cContent . $content;
}
// $content = $cContent . substr($content, - $position + $pos + 1);// 不太稳定,拼接字符串
$pos--;
if ($sep === $cContent) {
$i++;
}
if (fgets($handle) === false) {
break;
}
}
$pos++;
}
return array('content' => $content, 'pos' => $pos);
}
/**
* 检测字符编码并专成UTF-8格式
* @param $str
*
* @return string
*/
public function safeEncoding($str)
{
// $code = mb_detect_encoding($str, array('ASCII', 'GB2312', 'GBK', 'UTF-8'));
// if ($code == 'CP936') {
// return $str;
// }
return iconv('GBK', 'UTF-8', $str);
}
/**
* @param $url
*
* @return bool|string
*/
public function getHandleUrl($url)
{
$pos = strpos($url, '//');
$newUrl = substr($url, $pos + 2);
$lpos = strpos($newUrl, '/');
if ($lpos === false) {
return $newUrl;
}
return substr($newUrl, 0, $lpos);
}
/**
*
*/
public function __destruct()
{
// TODO: Implement __destruct() method.
}
}