@@ -178,10 +178,9 @@ public static function fileDelete(string $path): bool
178
178
* @return bool|array
179
179
* @throws Exception
180
180
*/
181
- public function writeUniqueLinesToFile (string $ inputFile , string $ outputFile = '' ): bool |array
181
+ public static function writeUniqueLinesToFile (string $ inputFile , string $ outputFile = '' ): bool |array
182
182
{
183
183
ini_set ('memory_limit ' , '-1 ' );
184
-
185
184
$ handle = null ;
186
185
$ outputHandle = null ;
187
186
$ hashSet = [];
@@ -190,7 +189,7 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
190
189
if (!$ handle ) {
191
190
throw new Exception ("打开输入文件失败: 输入文件: $ inputFile. 请检查文件是否存在且可读。 " );
192
191
}
193
- if (!is_null ($ outputFile )) {
192
+ if (!empty ($ outputFile )) {
194
193
$ directory = dirname ($ outputFile );
195
194
// 检查目录是否存在,不存在则创建
196
195
if (!is_dir ($ directory )) {
@@ -207,7 +206,7 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
207
206
$ line = trim ($ line );
208
207
if ($ line !== '' && !isset ($ hashSet [$ line ])) {
209
208
$ hashSet [$ line ] = true ; // 记录唯一值
210
- if (!is_null ($ outputFile )) {
209
+ if (!empty ($ outputFile )) {
211
210
fwrite ($ outputHandle , $ line . PHP_EOL ); // 写入输出文件
212
211
}
213
212
}
@@ -223,7 +222,7 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
223
222
}
224
223
}
225
224
// 返回结果
226
- if (! is_null ($ outputFile )) {
225
+ if (empty ($ outputFile )) {
227
226
return array_keys ($ hashSet ); // 返回唯一值的数组
228
227
} else {
229
228
return true ;
@@ -241,8 +240,9 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
241
240
* @return bool|array
242
241
* @throws Exception
243
242
*/
244
- function getCommonLinesFromFiles (array $ filePaths , string $ outputFile = '' ): bool |array
243
+ public static function getCommonLinesFromFiles (array $ filePaths , string $ outputFile = '' ): bool |array
245
244
{
245
+ ini_set ('memory_limit ' , '-1 ' );
246
246
$ commonLines = null ;
247
247
foreach ($ filePaths as $ filePath ) {
248
248
$ handle = fopen ($ filePath , 'r ' );
@@ -267,7 +267,7 @@ function getCommonLinesFromFiles(array $filePaths, string $outputFile = ''): boo
267
267
}
268
268
}
269
269
$ result = array_keys ($ commonLines );
270
- if (!is_null ($ outputFile )) {
270
+ if (!empty ($ outputFile )) {
271
271
$ tempFile = $ outputFile . '.tmp ' ;
272
272
file_put_contents ($ tempFile , implode (PHP_EOL , $ result ));
273
273
rename ($ tempFile , $ outputFile ); // 使用原子写入方式
@@ -285,11 +285,12 @@ function getCommonLinesFromFiles(array $filePaths, string $outputFile = ''): boo
285
285
* @param string $inputFolder 文件夹路径
286
286
* @param int $columnIndex 获取第几列数据,从0开始
287
287
* @param string $outputFile 输出文件路径,如果为空字符串,则不保存结果
288
+ * @param bool $skipHeader 是否跳过第一行标题行
288
289
*
289
290
* @return bool|array
290
291
* @throws Exception
291
292
*/
292
- function extractColumnFromCsvFiles (string $ inputFolder , int $ columnIndex , string $ outputFile = '' , bool $ skipHeader = true ): bool |array
293
+ public static function extractColumnFromCsvFiles (string $ inputFolder , int $ columnIndex , string $ outputFile = '' , bool $ skipHeader = true ): bool |array
293
294
{
294
295
$ hashSet = [];
295
296
try {
@@ -298,9 +299,11 @@ function extractColumnFromCsvFiles(string $inputFolder, int $columnIndex, string
298
299
throw new Exception ("输入文件夹不存在: $ inputFolder " );
299
300
}
300
301
// 打开输出文件(以追加模式写入)
301
- $ outputHandle = fopen ($ outputFile , 'a ' );
302
- if (!$ outputHandle ) {
303
- throw new Exception ("打开输出文件失败: $ outputFile " );
302
+ if (!empty ($ outputFile )) {
303
+ $ outputHandle = fopen ($ outputFile , 'a ' );
304
+ if (!$ outputHandle ) {
305
+ throw new Exception ("打开输出文件失败: $ outputFile " );
306
+ }
304
307
}
305
308
// 获取文件夹中的所有 CSV 文件
306
309
$ csvFiles = glob ("$ inputFolder/*.csv " );
@@ -326,7 +329,7 @@ function extractColumnFromCsvFiles(string $inputFolder, int $columnIndex, string
326
329
$ columnData = $ row [$ columnIndex ] ?? '' ;
327
330
// 如果数据存在且非空,写入到输出文件
328
331
if (!empty ($ columnData )) {
329
- if (!is_null ($ outputFile )) {
332
+ if (!empty ($ outputFile )) {
330
333
fwrite ($ outputHandle , $ columnData . PHP_EOL ); // 写入输出文件
331
334
} else {
332
335
$ hashSet [] = $ columnData ;
@@ -335,12 +338,14 @@ function extractColumnFromCsvFiles(string $inputFolder, int $columnIndex, string
335
338
}
336
339
fclose ($ handle );
337
340
}
338
- fclose ($ outputHandle );
341
+ if (!empty ($ outputFile )) {
342
+ fclose ($ outputHandle );
343
+ }
339
344
} catch (Exception $ e ) {
340
345
throw new Exception ("Error: " . $ e ->getMessage ());
341
346
}
342
347
// 返回数据
343
- if (!is_null ($ outputFile )) {
348
+ if (!empty ($ outputFile )) {
344
349
return true ;
345
350
} else {
346
351
return $ hashSet ;
0 commit comments