Jump to content

[SOLVED] Some Help Please?


mnybud

Recommended Posts

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?

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.