src/CsvLib.php
/**
     * Calculate the mathematical differences between two CSV files. If this comes across string 
     * values, it will check if they are the same and put the the string in if they are, otherwise,
     * it will concatenate the two values with a | divider.
     * WARNING - this expects the two files to have the same number of columns and rows.
     * @param string $filepath1 - path to a CSV file to compare.
     * @param string $filepath2 - path to a CSV file to compare.
     * @param string $delimiter - optionally specify the delimiter if it isn't a comma
     * @param string $enclosure - optionally specify the enclosure if it isn't double quote
     * @return string - the path to the created diff CSV file.
     * @throws \Exception
     */
Calculate the mathematical differences between two CSV files. If this comes across string
values, it will check if they are the same and put the the string in if they are, otherwise,
it will concatenate the two values with a | divider.
WARNING - this expects the two files to have the same number of columns and rows.
@param string $filepath1 - path to a CSV file to compare.
@param string $filepath2 - path to a CSV file to compare.
@param string $delimiter - optionally specify the delimiter if it isn't a comma
@param string $enclosure - optionally specify the enclosure if it isn't double quote
@return string - the path to the created diff CSV file.
@throws \Exception
Calculate the mathematical differences between two CSV files . If this comes across string values it will check if they are the same and put the the string in if they are otherwise it will concatenate the two values with a | divider . WARNING - this expects the two files to have the same number of columns and rows . @param string $filepath1 - path to a CSV file to compare . @param string $filepath2 - path to a CSV file to compare . @param string $delimiter - optionally specify the delimiter if it isn ' t a comma @param string $enclosure - optionally specify the enclosure if it isn ' t double quote @return string - the path to the created diff CSV file . @throws \ Exception
public static function decimalDiff($filepath1, $filepath2, $delimiter=",", $enclosure='"')
    {
        $newFileName = tempnam(sys_get_temp_dir(), "");
        $newFileHandle = fopen($newFileName, "w");
        if (!$newFileHandle)
        {
            throw new \Exception("Cannot create file to store diff in.");
        }
        $fileHandle1 = fopen($filepath1, "r");
        $fileHandle2 = fopen($filepath2, "r");
        $lineNumber = 0;
        if ($fileHandle1 && $fileHandle2)
        {
            while (!feof($fileHandle1))
            {
                $lineArray1 = fgetcsv($fileHandle1, 0, $delimiter, $enclosure);
                $lineArray2 = fgetcsv($fileHandle2, 0, $delimiter, $enclosure);
                $resultArray = array();
                if ($lineArray1)
                {
                    foreach ($lineArray1 as $index => $value1)
                    {
                        if (!isset($lineArray2[$index]))
                        {
                            throw new \Exception("decimalDiff: rows do not have the same column count.");
                        }
                        $value2 = $lineArray2[$index];
                        if (is_numeric($value1) && is_numeric($value2))
                        {
                            $resultArray[$index] = $value1 - $value2;
                        }
                        else
                        {
                            if ($value1 === $value2)
                            {
                                $resultArray[$index] = $value1;
                            }
                            else
                            {
                                $resultArray[$index] = $value1 . "|" . $value2;
                            }
                        }
                    }
                }
                fputcsv($newFileHandle, $resultArray, $delimiter, $enclosure);
                $lineNumber++;
            }
            fclose($fileHandle1);
            fclose($fileHandle2);
            fclose($newFileHandle);
            return $newFileName;
        }
        else
        {
            throw new \Exception("decimalDiff: Failed to open CSV file for processing.");
        }
        return $newFileName;
    }
public static function decimalDiff ( $ filepath1 , $ filepath2 , $ delimiter = " , " , $ enclosure = '"' ) { $ newFileName = tempnam ( sys_get_temp_dir ( ) , " " ) ; $ newFileHandle = fopen ( $ newFileName , " w " ) ; if ( ! $ newFileHandle ) { throw new \ Exception ( " Cannot create file to store diff in. " ) ; } $ fileHandle1 = fopen ( $ filepath1 , " r " ) ; $ fileHandle2 = fopen ( $ filepath2 , " r " ) ; $ lineNumber = 0 ; if ( $ fileHandle1 && $ fileHandle2 ) { while ( ! feof ( $ fileHandle1 ) ) { $ lineArray1 = fgetcsv ( $ fileHandle1 , 0 , $ delimiter , $ enclosure ) ; $ lineArray2 = fgetcsv ( $ fileHandle2 , 0 , $ delimiter , $ enclosure ) ; $ resultArray = array ( ) ; if ( $ lineArray1 ) { foreach ( $ lineArray1 as $ index => $ value1 ) { if ( ! isset ( $ lineArray2 [ $ index ] ) ) { throw new \ Exception ( " decimalDiff: rows do not have the same column count. " ) ; } $ value2 = $ lineArray2 [ $ index ] ; if ( is_numeric ( $ value1 ) && is_numeric ( $ value2 ) ) { $ resultArray [ $ index ] = $ value1 - $ value2 ; } else { if ( $ value1 === $ value2 ) { $ resultArray [ $ index ] = $ value1 ; } else { $ resultArray [ $ index ] = $ value1 . " | " . $ value2 ; } } } } fputcsv ( $ newFileHandle , $ resultArray , $ delimiter , $ enclosure ) ; $ lineNumber ++ ; } fclose ( $ fileHandle1 ) ; fclose ( $ fileHandle2 ) ; fclose ( $ newFileHandle ) ; return $ newFileName ; } else { throw new \ Exception ( " decimalDiff: Failed to open CSV file for processing. " ) ; } return $ newFileName ; }
Calculate the mathematical differences between two CSV files.
Calculate the mathematical differences between two CSV files .