Php deletes duplicate rows in a text file

Source: Internet
Author: User
This article describes how to delete duplicate rows in a text file in php. it involves some skills related to php operations on text files and is of great practical value, for more information about how to delete duplicate rows in a text file in php, see the following example. 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.

/** * 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:

// 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// should 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.

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.