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

Recommended Answers

All 2 Replies

Member Avatar for diafol

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()

commented: Perfect example +2

Thank you for the answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.