0

Hello. I have two text files. Text1.txt and Text2.txt. I am needing to remove an lines found in Text1.txt from Text2.txt. That was probably confusing, so let me give an example.

Let's say Text1.txt has this:
1
2
3

And Text2.txt has this:
2
3
4

The final result would be:

Text1.txt:
1
2
3

Text2.txt:
4

If this doesn't make sense, please let me know.

Thank you in advance,

Resentful

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by Resentful
1

One way to do this is to place both textfile lines into an array using file().

e.g. $t1 = file('text1.txt'); then you need to find the items in $t2 that don't exist in $t1:

use

$result = array_diff($t2,$t1);

Then implode the $result array with a '\n' and write back to text2.txt using something like file_put_contents()

Edited by diafol: n/a

Votes + Comments
Perfect example
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.