0

I have the following code:

$l1 = file($file1['tmp_name']);// get file 1 contents
$l2 = file($file2['tmp_name']);// get file 2 contents
$l3 = array_diff($l1, $l2);// create diff array

Here are the files: File 1:

6974527983
6974527984
6974527985

File 2:

6974527983

$l3 should be:

6974527984
6974527985

But, instead it is just spitting out the values from File 1:

6974527983
6974527984
6974527985

Am I setting this up right?

UPdate - Using print_r(), I have verified that the files being loaded are being properly parsed into arrays: File 1 -

Array ( [0] => 6974527983 [1] => 6974527984 [2] => 6974527985 ) 1

File 2 -

Array ( [0] => 6974527983 ) 1

So I don't believe there are any issues with the newlines in the text files.

4

3 回答 3

0

如果每个数字都在新行上,您可以尝试按换行符拆分每个文件并以这种方式比较数组。

$l1 = explode("\n", file($file1['tmp_name']));
$l2 = explode("\n", file($file2['tmp_name']));
$l3 = array_diff($l1, $l2);
于 2014-04-22T23:45:16.193 回答
0

使用以下示例,您可以看到它array_diff()按预期工作:

$a = array(
    6974527983,
    6974527984,
    6974527985
);

$b = array(
    6974527983
);

var_dump(array_diff($a, $b));

输出:

array(2) {
  [1] =>
  int(6974527984)
  [2] =>
  int(6974527985)
}

这表明这file($file2['tmp_name'])是您的问题。尝试:

var_dump(file($file2['tmp_name']));

检查文件的内容。

于 2014-04-23T00:04:40.690 回答
0

好的,我会发布答案,因为我认为这将解决您的问题。

在不了解文件结构的情况下,我们只能假设行尾可能存在问题。有三种可能的行尾:

  1. Unix:\n
  2. 视窗:\r\n
  3. 经典的mac:\r

我在这里看到两种可能的情况:

  1. 每个文件中的行尾彼此不同
  2. 两个文件中的行尾都是\r(classic mac)

正如马克贝克指出的那样,您应该使用FILE_IGNORE_NEW_LINES标志作为每个file()调用的第二个参数。据我在这里快速试验可以看出,如果一个文件有 Unix 而另一个文件有 Windows 行结尾,这应该可以解决问题。

但是,在至少一个文件有 '\r' 行结尾的情况下,它似乎不能很好地处理。在这种情况下,有一个 ini 设置可能会有所帮助:

ini_set('auto_detect_line_endings', true);

咨询文档auto_detect_line_endings

打开时,PHP 将检查 fgets() 和 file() 读取的数据,以查看它是否使用 Unix、MS-Dos 或 Macintosh 行尾约定。

这使 PHP 可以与 Macintosh 系统互操作,但默认为 Off,因为在检测第一行的 EOL 约定时性能损失非常小,而且在 Unix 系统下使用回车作为项目分隔符的人会遇到非向后兼容的行为。

所以,TL;DR:调试你的行尾以确保你知道发生了什么(使用filehexdump类似),并使用 and 的auto_detect_line_endings组合FILE_IGNORE_NEW_LINES

希望这可以帮助 :)

于 2014-04-23T00:41:11.660 回答