@@ -178,10 +178,9 @@ public static function fileDelete(string $path): bool
178178 * @return bool|array
179179 * @throws Exception
180180 */
181- public function writeUniqueLinesToFile (string $ inputFile , string $ outputFile = '' ): bool |array
181+ public static function writeUniqueLinesToFile (string $ inputFile , string $ outputFile = '' ): bool |array
182182 {
183183 ini_set ('memory_limit ' , '-1 ' );
184-
185184 $ handle = null ;
186185 $ outputHandle = null ;
187186 $ hashSet = [];
@@ -190,7 +189,7 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
190189 if (!$ handle ) {
191190 throw new Exception ("打开输入文件失败: 输入文件: $ inputFile. 请检查文件是否存在且可读。 " );
192191 }
193- if (!is_null ($ outputFile )) {
192+ if (!empty ($ outputFile )) {
194193 $ directory = dirname ($ outputFile );
195194 // 检查目录是否存在,不存在则创建
196195 if (!is_dir ($ directory )) {
@@ -207,7 +206,7 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
207206 $ line = trim ($ line );
208207 if ($ line !== '' && !isset ($ hashSet [$ line ])) {
209208 $ hashSet [$ line ] = true ; // 记录唯一值
210- if (!is_null ($ outputFile )) {
209+ if (!empty ($ outputFile )) {
211210 fwrite ($ outputHandle , $ line . PHP_EOL ); // 写入输出文件
212211 }
213212 }
@@ -223,7 +222,7 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
223222 }
224223 }
225224 // 返回结果
226- if (! is_null ($ outputFile )) {
225+ if (empty ($ outputFile )) {
227226 return array_keys ($ hashSet ); // 返回唯一值的数组
228227 } else {
229228 return true ;
@@ -241,8 +240,9 @@ public function writeUniqueLinesToFile(string $inputFile, string $outputFile = '
241240 * @return bool|array
242241 * @throws Exception
243242 */
244- function getCommonLinesFromFiles (array $ filePaths , string $ outputFile = '' ): bool |array
243+ public static function getCommonLinesFromFiles (array $ filePaths , string $ outputFile = '' ): bool |array
245244 {
245+ ini_set ('memory_limit ' , '-1 ' );
246246 $ commonLines = null ;
247247 foreach ($ filePaths as $ filePath ) {
248248 $ handle = fopen ($ filePath , 'r ' );
@@ -267,7 +267,7 @@ function getCommonLinesFromFiles(array $filePaths, string $outputFile = ''): boo
267267 }
268268 }
269269 $ result = array_keys ($ commonLines );
270- if (!is_null ($ outputFile )) {
270+ if (!empty ($ outputFile )) {
271271 $ tempFile = $ outputFile . '.tmp ' ;
272272 file_put_contents ($ tempFile , implode (PHP_EOL , $ result ));
273273 rename ($ tempFile , $ outputFile ); // 使用原子写入方式
@@ -285,11 +285,12 @@ function getCommonLinesFromFiles(array $filePaths, string $outputFile = ''): boo
285285 * @param string $inputFolder 文件夹路径
286286 * @param int $columnIndex 获取第几列数据,从0开始
287287 * @param string $outputFile 输出文件路径,如果为空字符串,则不保存结果
288+ * @param bool $skipHeader 是否跳过第一行标题行
288289 *
289290 * @return bool|array
290291 * @throws Exception
291292 */
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
293294 {
294295 $ hashSet = [];
295296 try {
@@ -298,9 +299,11 @@ function extractColumnFromCsvFiles(string $inputFolder, int $columnIndex, string
298299 throw new Exception ("输入文件夹不存在: $ inputFolder " );
299300 }
300301 // 打开输出文件(以追加模式写入)
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+ }
304307 }
305308 // 获取文件夹中的所有 CSV 文件
306309 $ csvFiles = glob ("$ inputFolder/*.csv " );
@@ -326,7 +329,7 @@ function extractColumnFromCsvFiles(string $inputFolder, int $columnIndex, string
326329 $ columnData = $ row [$ columnIndex ] ?? '' ;
327330 // 如果数据存在且非空,写入到输出文件
328331 if (!empty ($ columnData )) {
329- if (!is_null ($ outputFile )) {
332+ if (!empty ($ outputFile )) {
330333 fwrite ($ outputHandle , $ columnData . PHP_EOL ); // 写入输出文件
331334 } else {
332335 $ hashSet [] = $ columnData ;
@@ -335,12 +338,14 @@ function extractColumnFromCsvFiles(string $inputFolder, int $columnIndex, string
335338 }
336339 fclose ($ handle );
337340 }
338- fclose ($ outputHandle );
341+ if (!empty ($ outputFile )) {
342+ fclose ($ outputHandle );
343+ }
339344 } catch (Exception $ e ) {
340345 throw new Exception ("Error: " . $ e ->getMessage ());
341346 }
342347 // 返回数据
343- if (!is_null ($ outputFile )) {
348+ if (!empty ($ outputFile )) {
344349 return true ;
345350 } else {
346351 return $ hashSet ;
0 commit comments