<?php /*比较两个txt文件,格式如下,分别取出a.txt有的b.txt没有的,b.txt有的a.txt没有的及两个都有的 * a.txt: * A * B * C * D * b.txt * A * B * M * N * result: * only_a.txt * C * D * only_b.txt * M * N * public_a_b.txt * A * B */ $curr_file_name = dirname(__FILE__); if (file_exists($curr_file_name . ‘\a.txt‘)) { $array_a = explode("\r\n", trim(file_get_contents(‘a.txt‘))); } if (file_exists($curr_file_name . ‘\b.txt‘)) { $array_b = explode("\r\n", trim(file_get_contents(‘b.txt‘))); } if($array_a) { foreach ($array_a as $value) { if (!in_array($value, $array_b)) { $only_a[] = $value; } } file_put_contents("only_a.txt", implode("\r\n", $only_a)); } if($array_b) { $public_a_b = array(); foreach ($array_b as $value) { if (!in_array($value, $array_a)) { $only_b[] = $value; } else { $public_a_b[] = $value; } } file_put_contents("only_b.txt", implode("\r\n", $only_b)); file_put_contents("public_a_b.txt", implode("\r\n", $public_a_b)); } ?>
时间: 2024-10-09 20:56:23