mnybud Posted January 24, 2008 Share Posted January 24, 2008 I am trying to accomplish the following. I have very limited PHP knowledge so I don't know if this is even possible. Here is what I am tryign to do...... I have 2 text files of keywords. 1 keyword per line in each file. I want to compare the two text files and remove any duplicate terms found in both the lists. So for example. If terms1.txt has.... term1 term2 term3 term4 and terms2.txt has..... term2 term3 I would want a 3rd file generated from the comparison called terms3.txt which would only have term1 term4 Is this possible? Can anyone help me out? Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/ Share on other sites More sharing options...
Zhadus Posted January 24, 2008 Share Posted January 24, 2008 My suggestion would be to use arrays. Load the first text file into separates arrays. File 1 goes into Array1 and File 2 goes into Array2. Loop it to check each word in Array1 with the words in Array2, if no matches, pass into Array3. Then do it again with Array2 passing each word to Array1. Write Array3 to the 3rd text file. Hope you can understand that? Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448235 Share on other sites More sharing options...
mnybud Posted January 24, 2008 Author Share Posted January 24, 2008 a bit over my head Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448240 Share on other sites More sharing options...
PHP Monkeh Posted January 24, 2008 Share Posted January 24, 2008 If you manage to load the keywords of each file in to arrays, you could then search through them like this: <?php for($i=0; $i<count($array1); $i++) { $found = false; for($f=0; $f<count($array2); $i++) { if($array1[$i] == $array2[$f] && $found == false) { $found = true; } } if($found == false) { $array3 = $array1[$i]; } ?> Although I hope that works, it wasn't as simple as I thought when I started writing it! That should put all the keywords that don't appear in both lists in array3. Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448249 Share on other sites More sharing options...
mnybud Posted January 24, 2008 Author Share Posted January 24, 2008 thanks! ?How would i put them in arrays properly? Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448253 Share on other sites More sharing options...
kenrbnsn Posted January 24, 2008 Share Posted January 24, 2008 You should take a look at the array_diff() function. Steps: Read both files into arrays using the file() function Use array_diff() to create a third array. Write the third array to the new file. <?php $terms1 = file('terms1.txt'); $terms2 = file('terms2.txt'); $terms3 = array_diff($terms1,$terms2); $fp = fopen('terms3.txt','w'); fwrite($fp,implode('',$terms3)); fclose($fp); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448254 Share on other sites More sharing options...
PHP Monkeh Posted January 24, 2008 Share Posted January 24, 2008 Smite you ken ¬_¬ And my method was so (un)sophisticated! Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448258 Share on other sites More sharing options...
Zhadus Posted January 24, 2008 Share Posted January 24, 2008 Err, that was so much better than what I had ready to give him. Your code is hot Ken, very hot. Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448293 Share on other sites More sharing options...
mnybud Posted January 24, 2008 Author Share Posted January 24, 2008 ok gonna try it out. thanks a lot guys! Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448343 Share on other sites More sharing options...
mnybud Posted January 24, 2008 Author Share Posted January 24, 2008 ok so i set it up on my server but it doesnt seem to function properly. It creates the new file ... terms3.txt ... but it includes all the terms from terms1.txt and doesn't remove the ones listed in terms2.txt Do I need to modify this code further? <?php $terms1 = file('terms1.txt'); $terms2 = file('terms2.txt'); $terms3 = array_diff($terms1,$terms2); $fp = fopen('terms3.txt','w'); fwrite($fp,implode('',$terms3)); fclose($fp); ?> Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448349 Share on other sites More sharing options...
mnybud Posted January 24, 2008 Author Share Posted January 24, 2008 actually no, working great! I had a space after my terms causing a mismatch. Thanks so much guys!! Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/87636-solved-some-help-please/#findComment-448351 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.