PHP Remove duplicate rows in a text file _php tips

Source: Internet
Author: User

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

/** * 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 to the given file for ($Line =0; $Line < count ($Lines); $Line + +) {//Trim whitespace for th
    E 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 [$L    
    Inekey]) $LineArray [$LineKey] = $CurrentLine;
  else $Duplicates + +;
  }//Sort the array asort ($LineArray); Return to many lines got removed return implode ($NewLine, Array_values ($LineArray)); }

Use Example:

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 be Used as new line indicator. In this case
//The example file looks like ' Foo;bar;foo;foo ' and would
/is replaced with ' Foo;bar ' 
Remove Duplicatedlines (' 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.