จะมีประโยชน์ในการ compare ให้โปรแกรมเมอร์หรือผู้ที่ต้องการตรวจสอบแก้ไขไฟล์
ให้สามารถหาตำแหน่งแก้ไขเพิ่มเติมหรือแก้ปัญหาที่เกิดขึ้นได้อย่างสะดวกรวดเร็ว
ใช้คำสั่ง xdiff_file_diff ซึ่งต้อง download dll มาลงเป็น extension ก่อนครับที่ลิงค์
http://pecl.php.net/package/xdiff
<?php
function rec($path,$target,$diff){
$h = opendir($path);
$time = time();
$day = 60*60*24;
$dayAmount = $day*5;
while($file = readdir($h)){
if ($file != '.' && $file != '..' && $file != '.git' && $file != 'nbproject'){
if (is_dir($path.'/'.$file)){
rec($path.'/'.$file,$target.'/'.$file,$diff.'/'.$file);
}else{
$mtime = filemtime($target.'/'.$file);
if ($time - $mtime < $dayAmount){
$p1 = str_replace("\\",'/',$path).'/'.$file;
$p2 = str_replace("\\",'/',$target).'/'.$file;
$p3 = str_replace("\\",'/',$diff).'/'.$file;
$tdir = dirname($p3);
@mkdir($tdir,0777,true);
xdiff_file_diff($p1, $p2, $p3, 2);
echo "<pre>\r\n=====================================================\r\n\r\n";
echo '<strong>',str_replace('C:/wamp/www/diff','',$p3)."</strong><br>";
$a = file($p3);
foreach($a as $line){
if (substr($line,0,1)=='-'){
echo '<span style="color:red">',htmlspecialchars(substr($line,1)),'</span>';
}
elseif (substr($line,0,1)=='+'){
echo '<span style="color:limegreen">',htmlspecialchars(substr($line,1)),'</span>';
}
elseif (substr($line,0,2)=='@@'){
preg_match('/^@@ \-\d+\,\d+ \+(\d+)\,\d+/',$line,$matches);
echo '<span style="color:#6495ED;">บรรทัดที่ '.$matches[1],'</span>';
}
else{
echo htmlspecialchars($line);
}
}
echo '</pre>';
}
}
}
}
}
header('Content-type:text/html;charset=utf-8');
rec('C:\wamp\www\init-code','C:\wamp\www\update-code','C:\wamp\www\diff');