How PHP deletes duplicate rows in a text file

Source: Internet
Author: User
Tags bool trim

how PHP deletes duplicate rows in a text file

The example in this article describes how PHP deletes duplicate rows in a text file. Share to everyone for your reference. The specific analysis is as follows:

This PHP function is used to delete duplicate rows in a file, to specify whether to ignore case, and to specify line breaks

?

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 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 T Ext 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 '. $Filepat H. ' 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 to the given file for ($Line =0 $Line < count ($Lines); $Line + +) {//Trim whitespace for the Curr ENT 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 to how many lines got removed return implode ($NewLine, Array_values ($LineArray)); }

Use 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" $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 can define which character//should as New Line indicator. In this case//The example file looks like ' Foo;bar;foo;foo ' and would/is replaced with ' Foo;bar ' removeduplicatedlines (' Test.txt ', false, '; ');

I hope this article will help you with your PHP program design.

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.