-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnsetUtf8Bom.php
More file actions
161 lines (146 loc) · 3.71 KB
/
UnsetUtf8Bom.php
File metadata and controls
161 lines (146 loc) · 3.71 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
<?php
/**
* 遍历文件夹及子文件夹文件,清除utf8+bom头(0xEF 0xBB 0xBF)
* 继承 AbstractFindFile 执行遍历文件夹处理
*
* @author fdipzone
* @DateTime 2023-08-01 16:47:12
*
*/
class UnsetUtf8Bom extends AbstractFindFile
{
/**
* 设置要处理的文件类型(文件后缀)
*
* @var array
*/
private $file_type = [];
/**
* 已处理的文件
*
* @var array
*/
private $files = [];
/**
* 初始化,设置需要处理的文件类型
*
* @author fdipzone
* @DateTime 2023-08-01 16:56:51
*
* @param array $file_type 文件类型列表(文件后缀)
*/
public function __construct(array $file_type=[])
{
if($file_type)
{
$this->file_type = $file_type;
}
}
/**
* 输出已执行处理的文件列表
*
* @author fdipzone
* @DateTime 2023-08-01 17:40:29
*
* @return array
*/
public function response():array
{
return $this->files;
}
/**
* 对文件执行处理
* 实现父类的抽象方法
*
* @author fdipzone
* @DateTime 2023-08-01 16:49:05
*
* @param string $file 文件
* @return void
*/
protected function process(string $file):void
{
// 检查文件类型是否需要处理及是否包含utf8+Bom
if($this->checkFileExtension($file) && $this->checkUtf8Bom($file))
{
// 清除文件的utf8+Bom
$this->clearUtf8Bom($file);
// 记录到已处理文件列表
$this->files[] = $file;
}
}
/**
* 检查文件是否带有utf8+bom头
*
* @author fdipzone
* @DateTime 2023-08-01 16:59:19
*
* @param string $file 文件
* @return boolean
*/
private function checkUtf8Bom(string $file):bool
{
$is_utf8_bom = false;
// 读取文件头三个字节
$fp = fopen($file, 'rb');
if($fp)
{
if(filesize($file)>=3)
{
$file_header = fread($fp, 3);
if(ord(substr($file_header,0,1))===0xEF && ord(substr($file_header,1,1))===0xBB && ord(substr($file_header,2,1))===0xBF)
{
$is_utf8_bom = true;
}
}
fclose($fp);
}
return $is_utf8_bom;
}
/**
* 清除文件utf8+bom头
*
* @author fdipzone
* @DateTime 2023-08-01 17:00:38
*
* @param string $file 带有utf8+bom头的文件
* @return void
*/
private function clearUtf8Bom(string $file):void
{
$fp = fopen($file, 'r+b');
if($fp)
{
// 将文件指针移动到文件开头
fseek($fp, 3);
// 读取文件指针当前位置之后的内容
$content = fread($fp, filesize($file)-3);
// 将文件指针重置到文件开头
fseek($fp, 0);
// 写入文件
fwrite($fp, $content);
// 在当前位置截断,移除截断位置之后的内容
ftruncate($fp, ftell($fp));
// 关闭文件
fclose($fp);
}
else
{
throw new \Exception(sprintf('file: %s can not open', $file));
}
}
/**
* 检查是否需要处理的文件类型
*
* @author fdipzone
* @DateTime 2023-08-01 17:17:01
*
* @param string $file 文件
* @return boolean
*/
private function checkFileExtension(string $file):bool
{
$extension = strtolower(substr($file, strrpos($file, '.')+1));
return in_array($extension, $this->file_type)? true : false;
}
}