PHP Action text File delete duplicate lines

Source: Internet
Author: User
This article mainly introduces the PHP operation text file deletion duplicate line, the interest friend's reference, hoped to be helpful to everybody.

The example in this article describes how PHP deletes duplicate rows in a text file. The specific analysis is as follows:

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

/** * Removeduplicatedlines * This function removes all duplicated lines of the given text file. * * @param String * @param bool * @return string */function removeduplicatedlines ($Filepath, $IgnoreCase =false, $NewL    Ine= "\ 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)) $Line  s = 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 [$Lin        EKey]) $LineArray [$LineKey] = $CurrentLine;  else $Duplicates + +;  }//Sort the array asort ($LineArray);  Return how many lines got removed return implode ($NewLine, Array_values ($LineArray)); }

Examples of Use:

Example 1//removes all duplicated lines of the file definied in the first parameter. $RemovedLinesCount = Removeduplica Tedlines (' test.txt ');p rint "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 Defi Ne which character//should is used as new line indicator. The case//The example file looks like ' Foo;bar;foo;foo ' and will//is replaced with ' Foo;bar ' Removeduplicatedlines (' Test.txt ', false, '; ');

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

Common methods for handling PHP exceptions

Two ways to merge PHP arrays

PHP operation session and database method

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.