標籤:blog ar for 檔案 div on log as res
<?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));}?>
php:比較兩個txt檔案,格式如下,分別取出a.txt有的b.txt沒有的,b.txt有的a.txt沒有的及兩個都有的