Heim  >  Artikel  >  Backend-Entwicklung  >  PHP-Operationstextdatei zum Löschen doppelter Zeilen

PHP-Operationstextdatei zum Löschen doppelter Zeilen

墨辰丷
墨辰丷Original
2018-06-11 13:41:592194Durchsuche

In diesem Artikel wird hauptsächlich beschrieben, wie Sie mit PHP doppelte Zeilen in Textdateien löschen. Ich hoffe, dass er für Sie hilfreich ist.

Das Beispiel in diesem Artikel beschreibt, wie Sie doppelte Zeilen in Textdateien in PHP löschen. Die spezifische Analyse lautet wie folgt:

Diese PHP-Funktion wird verwendet, um doppelte Zeilen in der Datei zu löschen. Sie können auch angeben, ob die Groß-/Kleinschreibung ignoriert werden soll, und das Zeilenumbruchzeichen angeben

/**
 * 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 == &#39;&#39;)
      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));  
}

Verwendungsbeispiel:

// Example 1
// Removes all duplicated lines of the file definied in the first parameter.
$RemovedLinesCount = RemoveDuplicatedLines(&#39;test.txt&#39;);
print "Removed $RemovedLinesCount duplicate lines from the test.txt file.";
// Example 2 (Ignore case)
// Same as above, just ignores the line case.
RemoveDuplicatedLines(&#39;test.txt&#39;, 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 &#39;foo;bar;foo;foo&#39; and will
// be replaced with &#39;foo;bar&#39; 
RemoveDuplicatedLines(&#39;test.txt&#39;, false, &#39;;&#39;);

Zusammenfassung: Das Obige ist der gesamte Inhalt dieses Artikels, ich hoffe, dass er für das Studium aller hilfreich sein wird.

Verwandte Empfehlungen:

Gemeinsame PHP-Ausnahmebehandlungsmethoden

Zwei Methoden der PHP-Array-Fusion

Wie man Sitzung und Datenbank in PHP betreibt

Das obige ist der detaillierte Inhalt vonPHP-Operationstextdatei zum Löschen doppelter Zeilen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn