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.