国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > 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.  * RemoveDuplicatedLinesByString 
  30.  * This function removes all duplicated lines of the given string. 
  31.  * 
  32.  * @param   string 
  33.  * @param   bool 
  34.  * @return  string 
  35.  */ 
  36. function RemoveDuplicatedLinesByString($Lines$IgnoreCase=false, $NewLine="/n"){ 
  37.   if (is_array($Lines)) 
  38.     $Lines = implode($NewLine$Lines); 
  39.   $Lines = explode($NewLine$Lines); 
  40.   $LineArray = array(); 
  41.   $Duplicates = 0; 
  42.   // Go trough all lines of the given file 
  43.   for ($Line=0; $Line < count($Lines); $Line++){ 
  44.     // Trim whitespace for the current line 
  45.     $CurrentLine = trim($Lines[$Line]); 
  46.     // Skip empty lines 
  47.     if ($CurrentLine == ''
  48.       continue
  49.     // Use the line contents as array key 
  50.     $LineKey = $CurrentLine
  51.     if ($IgnoreCase
  52.       $LineKey = strtolower($LineKey); 
  53.     // Check if the array key already exists, 
  54.     // if not add it otherwise increase the counter 
  55.     if (!isset($LineArray[$LineKey])) 
  56.       $LineArray[$LineKey] = $CurrentLine;     
  57.     else        
  58.       $Duplicates++; 
  59.   } 
  60.   // Sort the array 
  61.   asort($LineArray); 
  62.   // Return how many lines got removed 
  63.   return implode($NewLinearray_values($LineArray));   

使用范例:

  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, ';');

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湾仔区| 遂川县| 嘉祥县| 福贡县| 株洲市| 正镶白旗| 尚志市| 马边| 车险| 晋中市| 昔阳县| 贺兰县| 同心县| 蕲春县| 霸州市| 永宁县| 莲花县| 右玉县| 蓝田县| 辽阳市| 那坡县| 辉南县| 土默特右旗| 沈阳市| 襄垣县| 双牌县| 错那县| 南召县| 凌云县| 城市| 乐业县| 玉林市| 海阳市| 会宁县| 新竹县| 汝州市| 宾川县| 博野县| 馆陶县| 长葛市| 通化县|