. // //-------------------- // // Script Description: // // A simple PHP diff script I wrote for no particular reason. Its // output is similar to how Wikipedia does it, though admittedly, a // lot simpler and inferior. It works really well for predictable // comparisons, like two files that are mostly the same or a directory // listing where some files have been renamed, added or removed. // Display source code if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) { if (isset ($_GET['source'])) { header ('Content-type: text/plain; charset=UTF-8'); exit (file_get_contents (basename (__FILE__))); } } // Replace '<' and '>' Characters function replace ($input) { return str_replace (array ('<', '>'), array ('<', '>'), $input); } // Read files $cf = replace (file_get_contents ('testdoc.txt')); // Current Version $of = replace (file_get_contents ('testdoc_old.txt')); // Old Version // Line Arrays $cv = explode ("\n", $cf); $ov = explode ("\n", $of); // Count Lines - Set to Longer Version $lc = (count ($cv) > count ($ov)) ? count ($cv) : count ($ov); // Fix Mismatched Line Counts for ($flc = count ($ov); $flc < $lc; $flc++) { $ov[$flc] = ''; } // Begin HTML Table echo '
', "\nCurrent Version: \n \n"; for ($l = 0; $l < $lc; $l++) { // Word Arrays $cw = explode (' ', $cv[$l]); // Current Version $ow = explode (' ', $ov[$l]); // Old Version // Count Words - Set to Longer Version $wc = (count ($cw) > count ($ow)) ? count ($cw) : count ($ow); // Fix Mismatched Word Counts for ($fwc = count ($ow); $fwc < $wc; $fwc++) { $ow[$fwc] = ''; } // If each line is identical, just echo the normal line. If not, // check if each word is identical. If not, wrap colored "" // tags around the mismatched words. if ($cv[$l] !== $ov[$l]) { for ($w = 0; $w < $wc; $w++) { if ($cw[$w] === $ow[$w]) { echo $cw[$w]; echo ($w !== ($wc - 1)) ? ' ' : "\n"; } else { echo '', $cw[$w]; echo ($w !== ($wc - 1)) ? ' ' : "\n"; } } } else { echo $cv[$l], "\n"; } } // End diff column echo "\n | \n\n"; // Begin old version column echo ' | ', "\nOld Version: \n \n"; echo $of, "\n"; // End old version column echo "\n | \n";
// End HTML table
echo "