首页 > 语言 > PHP > 正文

php删除文本文件中重复行的方法

2024-09-04 11:42:42
字体:大 中 小
来源:转载
供稿:网友

本文实例讲述了php删除文本文件中重复行的方法,分享给大家供大家参考,具体分析如下:

这个php函数用来删除文件中的重复行,还可以指定是否忽略大小写,和指定换行符:

  1. /** 
  2.  * RemoveDuplicatedLines 
  3.  * This function removes all duplicated lines of the given text file. 
  4.  * 
  5.  * @param   string 
  6.  * @param   bool 
  7.  * @return  string 
  8.  */ 
  9. function RemoveDuplicatedLines($Filepath, $IgnoreCase=false, $NewLine="/n"){ 
  10.   if (!file_exists($Filepath)){ 
  11.     $ErrorMsg = 'RemoveDuplicatedLines error: '; 
  12.     $ErrorMsg .= 'The given file ' . $Filepath . ' does not exist!'; 
  13.     die($ErrorMsg); 
  14.   } 
  15.   $Content = file_get_contents($Filepath); 
  16.   $Content = RemoveDuplicatedLinesByString($Content, $IgnoreCase, $NewLine); 
  17.   // Is the file writeable? 
  18.   if (!is_writeable($Filepath)){ 
  19.     $ErrorMsg = 'RemoveDuplicatedLines error: '; 
  20.     $ErrorMsg .= 'The given file ' . $Filepath . ' is not writeable!';   
  21.     die($ErrorMsg); 
  22.   } 
  23.   // Write the new file 
  24.   $FileResource = fopen($Filepath, 'w+');    
  25.   fwrite($FileResource, $Content);     
  26.   fclose($FileResource);   
  27. } 
  28.    
  29. /** 
  30.  * RemoveDuplicatedLinesByString 
  31.  * This function removes all duplicated lines of the given string. 
  32.  * 
  33.  * @param   string 
  34.  * @param   bool 
  35.  * @return  string 
  36.  */ 
  37. function RemoveDuplicatedLinesByString($Lines, $IgnoreCase=false, $NewLine="/n"){ 
  38.   if (is_array($Lines)) 
  39.     $Lines = implode($NewLine, $Lines); 
  40.   $Lines = explode($NewLine, $Lines); 
  41.   $LineArray = array(); 
  42.   $Duplicates = 0; 
  43.   // Go trough all lines of the given file 
  44.   for ($Line=0; $Line < count($Lines); $Line++){ 
  45.     // Trim whitespace for the current line 
  46.     $CurrentLine = trim($Lines[$Line]); 
  47.     // Skip empty lines 
  48.     if ($CurrentLine == '') 
  49.       continue; 
  50.     // Use the line contents as array key 
  51.     $LineKey = $CurrentLine; 
  52.     if ($IgnoreCase) 
  53.       $LineKey = strtolower($LineKey); 
  54.     // Check if the array key already exists, 
  55.     // if not add it otherwise increase the counter 
  56.     if (!isset($LineArray[$LineKey])) 
  57.       $LineArray[$LineKey] = $CurrentLine;     
  58.     else        
  59.       $Duplicates++; 
  60.   } 
  61.   // Sort the array 
  62.   asort($LineArray); 
  63.   // Return how many lines got removed 
  64.   return implode($NewLine, array_values($LineArray));   
  65. } 

使用范例:

  1. // Example 1 
  2. // Removes all duplicated lines of the file definied in the first parameter. 
  3. $RemovedLinesCount = RemoveDuplicatedLines('test.txt'); 
  4. print "Removed $RemovedLinesCount duplicate lines from the test.txt file."; 
  5. // Example 2 (Ignore case) 
  6. // Same as above, just ignores the line case. 
  7. RemoveDuplicatedLines('test.txt', true); 
  8. // Example 3 (Custom new line character) 
  9. // By using the 3rd parameter you can define which character 
  10. // should be used as new line indicator. In this case 
  11. // the example file looks like 'foo;bar;foo;foo' and will 
  12. // be replaced with 'foo;bar'  
  13. RemoveDuplicatedLines('test.txt', false, ';');

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表