比较PHP中的2 csv文件的内容 [英] Compare contents of 2 csv Files in PHP

查看:153
本文介绍了比较PHP中的2 csv文件的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道什么是比较2 csv文件的内容和报告相同的行的最佳方式。

Does anybody know what is the best way to compare the contents of 2 csv files and report the identical rows.

相同的意思是,具有相同的

By identical i mean, records which have the same values for each column.

推荐答案

rlCH的代码示例有一点问题,即

There's a bit of issue with the code example by rlCH, namely


  • 不能处理多行csv

  • 只能处理一个方向的差异

  • 停止在第一个区别

虽然它可能足够的操作我正在寻找一种方式比较两个多行csv文件。 (多行包含跨越多行的数据)所以我花了时间实际创建一个,我想为什么不共享它。也许它为某人节省了一些时间。

While it maybe enough for the op I was looking for a way to compare two multi-line csv files properly. (multi-line as in containing data spanning over multiple lines) So I've spent the time actually creating one, and I thought why not share it. Maybe it saves a bit of time for someone.

现在,我不是从命令行使用PHP,所以如果你想这样做,我建议你更改输入处理和输出(这一个输出html,所以你可以在浏览器中使用它)

Now, I'm not using PHP from command line, so if you want to do that I suggest you change the input handling and the output (this one outputs html so you can use it in the browser)

用法;
将脚本和文件放在目录中进行比较
使用两个参数调用脚本f1和f2
例如compareCSV.php?f1 = file1.csv& f2 = file2.csv

Usage; put the script and the files to compare in a directory call the script with two parameters, f1 and f2 eg compareCSV.php?f1=file1.csv&f2=file2.csv

<?php

//---- init
$strFileName1=isset($_REQUEST['f1'])?$_REQUEST['f1']:'';
$strFileName2=isset($_REQUEST['f2'])?$_REQUEST['f2']:'';

if ( !$strFileName1 ) { die("I need the first file (f1)"); }
if ( !$strFileName2 ) { die("I need the second file (f2)"); }

try {
    $arrFile1 = parseData($strFileName1);
    $arrFile2 = parseData($strFileName2);
} catch (Exception $e) {
    die($e->getMessage());
}

$rowCount1=count($arrFile1);
$rowCount2=count($arrFile2);

$colCount1=count($arrFile1[0]);
$colCount2=count($arrFile2[0]);

$highestRowCount = $rowCount1>$rowCount2 ? $rowCount1:$rowCount2;
$highestColCount = $colCount1>$colCount2 ? $colCount1:$colCount2;

$row = 0;
$err = 0;

//---- code

echo "<h2>comparing $strFileName1 and $strFileName2</h2>";
echo "\n<table border=1>";
echo "\n<tr><th>Err<th>Row#<th>Col#<th>Data in $strFileName1<th>Data in $strFileName2";
while($row<$highestRowCount) {
    if(!isset($arrFile1[$row])) {
        echo "\n<tr><td>Row missing in $strFileName1<th>$row";
        $err++;
    } elseif(!isset($arrFile1[$row])) {
        echo "\n<tr><td>Row missing in $strFileName2<th>$row";
        $err++;
    } else {
        $col=0;
        while($col<$highestColCount) {
            if ( !isset($arrFile1[$row][$col]) ) {
                echo "\n<tr><td>Data missing in $strFileName1<td>$row<td>$col<td><td>".htmlentities($arrFile2[$row][$col]);
                $err++;
            } elseif ( !isset($arrFile2[$row][$col]) ) {
                echo "\n<tr><td>Data missing in $strFileName1<td>$row<td>$col<td>".htmlentities($arrFile1[$row][$col]) ."<td>";
                $err++;
            } elseif ( $arrFile1[$row][$col] != $arrFile2[$row][$col] ) {
                echo "\n<tr><td>Data mismatch";
                echo "<td>$row <td>$col";
                echo "<td>".htmlentities($arrFile1[$row][$col]);
                echo "<td>".htmlentities($arrFile2[$row][$col]);
                $err++;
            }
            $col++;
        }
    }
    $row++;
}
echo "</table>";

if ( !$err ) {
    echo "<br/>\n<br/>\nThe two csv data files seem identical<br/>\n";
} else {
    echo "<br/>\n<br/>\nThere are $err differences";
}


//---- functions

function parseData($strFilename) {
    $arrParsed = array();
    $handle = fopen($strFilename , "r");
    if ($handle) {
        while (!feof($handle)) {
            $data = fgetcsv($handle , 0 , ',' , '"' );
            if ( empty($data)) continue; //empty row
            $arrParsed[]=$data;
        }
        fclose($handle);
    } else {
        throw new Exception("File read error at $strFilename");
    }
    return $arrParsed;
}

?>

这篇关于比较PHP中的2 csv文件的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆