raimis100 Posted July 29, 2008 Share Posted July 29, 2008 Hey! I am thinking of way to remove duplicate lines in my txt file each sentence is in the different line So far I did is <?php $list = file ('sentencelist.txt'); For ($I = 0; $I <= count ($list) - 1; $I++) { $dub = false; $corrent = trim ($list [$I]); For ($X = $I + 1; $X <= count ($list) - 1; $X++) { If ($corrent == trim ($list [$X])) { $dub = true; break; } If ($dub == False) { $output = $output . $corrent ."\n"; } } } echo $output; ?> basically what it does is compare each line but it crashed my apache because my txt files are huge [ around 100 k lines] Can anybody suggest me better way to do it edit: actually I found a bug. Script should be <?php $list = file ('sentencelist.txt'); For ($I = 0; $I <= count ($list) - 1; $I++) { $dub = false; $corrent = trim ($list [$I]); For ($X = $I + 1; $X <= count ($list) - 1; $X++) { If ($corrent == trim ($list [$X])) { $dub = true; break; } } If ($dub == False) { $output = $output . $corrent ."\n"; } } echo $output; ?> But there is still high cpu usage. Link to comment https://forums.phpfreaks.com/topic/117202-solved-remove-dublicated/ Share on other sites More sharing options...
lemmin Posted July 29, 2008 Share Posted July 29, 2008 See if this works: $nodups = array_unique(explode("\n", file_get_contents("sentencelist.txt"))); Link to comment https://forums.phpfreaks.com/topic/117202-solved-remove-dublicated/#findComment-602878 Share on other sites More sharing options...
raimis100 Posted July 29, 2008 Author Share Posted July 29, 2008 Hmm ... And you did with one line of code Yea. This wil work for me. Thanks! Link to comment https://forums.phpfreaks.com/topic/117202-solved-remove-dublicated/#findComment-602880 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.