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