Php: How to delete duplicate rows in a text file _ PHP Tutorial

Source: Internet
Author: User
Php deletes duplicate rows in a text file. Php: How to delete duplicate rows in a text file this article describes how to delete duplicate rows in a text file in php. I would like to share with you how to delete repeated rows in text files in php.

 Php deletes duplicate rows in a text file

This example describes how to delete duplicate rows in a text file in php. Share it with you for your reference. The specific analysis is as follows:

This php function is used to delete duplicate rows in the file. you can also specify whether to ignore the case sensitivity and specify the line break.

?

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 = '')

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

}

Example:

?

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('test.txt ');

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

// Example 2 (Ignore case)

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

RemoveDuplicatedLines('test.txt ', true );

// Example 3 (Custom new line character)

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

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

// The example file looks like 'foo; bar; foo; foo' and will

// Be replaced with 'Foo; bar'

RemoveDuplicatedLines('test.txt ', false ,';');

I hope this article will help you with php programming.

Using php to delete duplicate rows in a text file this article describes how php deletes duplicate rows in a text file. Share it with you for your reference...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.