Heim  >  Artikel  >  Backend-Entwicklung  >  PHP verarbeitet wiederholte Zeilen in Textdokumenten

PHP verarbeitet wiederholte Zeilen in Textdokumenten

*文
*文Original
2017-12-26 14:22:121456Durchsuche

Wie geht PHP mit wiederholten Zeilen in Textdokumenten um? In diesem Artikel wird hauptsächlich die Methode zum Löschen doppelter Zeilen in Textdateien in PHP vorgestellt und die damit verbundenen Fähigkeiten zum Betrieb von PHP-Textdateien behandelt. Ich hoffe, es hilft allen.

Das Beispiel in diesem Artikel beschreibt, wie Sie doppelte Zeilen in Textdateien in PHP löschen. Teilen Sie es als Referenz mit allen. 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));  
}


Anwendungsbeispiel:


// 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;);

Verwandte Empfehlungen:

PHP-Datei lesen fread, fgets, fgetc, file_get_contents und Beispielcode für die Verwendung von Dateifunktionen

Besprechen Sie kurz die PHP-Datei sperren

PHP-Dateidurchquerung

Das obige ist der detaillierte Inhalt vonPHP verarbeitet wiederholte Zeilen in Textdokumenten. 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