首頁 後端開發 php教程 php操作文字檔刪除重複行

php操作文字檔刪除重複行

Jun 11, 2018 pm 01:41 PM
php 刪除 文字檔案

這篇文章主要介紹php操作文字檔刪除重複行,有興趣的朋友參考下,希望對大家有幫助。

本文實例講述了php刪除文字檔案中重複行的方法。具體分析如下:

這個php函數用來刪除檔案中的重複行,也可以指定是否忽略大小寫,和指定換行符

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

/**

 * RemoveDuplicatedLines

 * This function removes all duplicated lines of the given text file.

 *

 * @param   string

 * @param   bool

 * @return  string

 */

function RemoveDuplicatedLines($Filepath, $IgnoreCase=false, $NewLine="\n"){

  if (!file_exists($Filepath)){

    $ErrorMsg = 'RemoveDuplicatedLines error: ';

    $ErrorMsg .= 'The given file ' . $Filepath . ' does not exist!';

    die($ErrorMsg);

  }

  $Content = file_get_contents($Filepath);

  $Content = RemoveDuplicatedLinesByString($Content, $IgnoreCase, $NewLine);

  // Is the file writeable?

  if (!is_writeable($Filepath)){

    $ErrorMsg = 'RemoveDuplicatedLines error: ';

    $ErrorMsg .= 'The given file ' . $Filepath . ' is not writeable!'; 

    die($ErrorMsg);

  }

  // Write the new file

  $FileResource = fopen($Filepath, 'w+');  

  fwrite($FileResource, $Content);   

  fclose($FileResource); 

}

  

/**

 * RemoveDuplicatedLinesByString

 * This function removes all duplicated lines of the given string.

 *

 * @param   string

 * @param   bool

 * @return  string

 */

function RemoveDuplicatedLinesByString($Lines, $IgnoreCase=false, $NewLine="\n"){

  if (is_array($Lines))

    $Lines = implode($NewLine, $Lines);

  $Lines = explode($NewLine, $Lines);

  $LineArray = array();

  $Duplicates = 0;

  // Go trough all lines of the given file

  for ($Line=0; $Line < count($Lines); $Line++){

    // Trim whitespace for the current line

    $CurrentLine = trim($Lines[$Line]);

    // Skip empty lines

    if ($CurrentLine == &#39;&#39;)

      continue;

    // Use the line contents as array key

    $LineKey = $CurrentLine;

    if ($IgnoreCase)

      $LineKey = strtolower($LineKey);

    // Check if the array key already exists,

    // if not add it otherwise increase the counter

    if (!isset($LineArray[$LineKey]))

      $LineArray[$LineKey] = $CurrentLine;   

    else       

      $Duplicates++;

  }

  // Sort the array

  asort($LineArray);

  // Return how many lines got removed

  return implode($NewLine, array_values($LineArray)); 

}

登入後複製

使用範例:

1

2

3

4

5

6

7

8

9

10

11

12

13

// Example 1

// Removes all duplicated lines of the file definied in the first parameter.

$RemovedLinesCount = RemoveDuplicatedLines(&#39;test.txt&#39;);

print "Removed $RemovedLinesCount duplicate lines from the test.txt file.";

// Example 2 (Ignore case)

// Same as above, just ignores the line case.

RemoveDuplicatedLines(&#39;test.txt&#39;, true);

// Example 3 (Custom new line character)

// By using the 3rd parameter you can define which character

// should be used as new line indicator. In this case

// the example file looks like &#39;foo;bar;foo;foo&#39; and will

// be replaced with &#39;foo;bar&#39;

RemoveDuplicatedLines(&#39;test.txt&#39;, false, &#39;;&#39;);

登入後複製

總結:以上就是這篇文章的全部內容,希望能對大家的學習有所幫助。

相關推薦:

常見的php異常處理方法

PHP陣列融合的兩種方法

php操作session及資料庫的方法

#

以上是php操作文字檔刪除重複行的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南

CakePHP 專案配置 CakePHP 專案配置 Sep 10, 2024 pm 05:25 PM

CakePHP 專案配置

CakePHP 日期和時間 CakePHP 日期和時間 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和時間

CakePHP 檔案上傳 CakePHP 檔案上傳 Sep 10, 2024 pm 05:27 PM

CakePHP 檔案上傳

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

討論 CakePHP 討論 CakePHP Sep 10, 2024 pm 05:28 PM

討論 CakePHP

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 如何設定 Visual Studio Code (VS Code) 進行 PHP 開發 Dec 20, 2024 am 11:31 AM

如何設定 Visual Studio Code (VS Code) 進行 PHP 開發

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

See all articles