|
 |
|
ติดตรง Compare ไฟล์ txt แล้ว Highlight ตรงตัวอักษรคำที่ไม่เหมือนกันไม่ได้ค่ะ |
|
 |
|
|
 |
 |
|
Index.php
Code (PHP)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> </title>
<script language="JavaScript" type="text/javascript">
function onSubmit() {
if(document.frmMain.mainFile.value == ''){
alert(" Select main file ");
document.frmMain.mainFile.focus();
return false;
}
if(document.frmMain.fileToCompare.value == ''){
alert(" Select compared file ");
document.frmMain.fileToCompare.focus();
return false;
}
}
</script>
</head>
<body>
<br/>
<form id="frmMain" name="frmMain" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<table border="0" width="100%" cellspacing="0" cellpadding="0" style="border-top: medium solid #000000;border-right: medium solid #000000;border-left: medium solid #000000;border-bottom: medium solid #000000">
<tr >
<td width="50%" align="center" bgcolor="#ccddff">
<font face="verdana" size="2" ><b>Select main file: </b></font>
<input type="file" name="mainFile"/><?php $ifile1 = $_FILES["mainFile"]; print_r($ifile1['name']);?>
</td>
<td width="50%" align="center" bgcolor="#ffccdd">
<font face="verdana" size="2" ><b>Select compared file: </b></font>
<input type="file" name="fileToCompare"/><?php $ifile2 = $_FILES["mainFile"]; print_r($ifile2['name']);?>
</td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Comparison" name="submitButton" onclick="return onSubmit()"/>
<input type="button" onclick="parent.location='<?php echo $_SERVER['SCRIPT_NAME'];?>' " value="Reset" />
</td>
</tr>
<tr>
<td colspan='2'>
</td>
</tr>
</table>
</form>
<?PHP
if( isset($_POST['submitButton'])) {
$ifile1 = $_FILES["mainFile"];
$info1 = pathinfo($ifile1['name']);
$filetype1 = strtolower($info1['extension']);
$ifile2 = $_FILES["fileToCompare"];
$info2 = pathinfo($ifile2['name']);
$filetype2 = strtolower($info2['extension']);
if($filetype1 == 'txt' || $filetype1 == 'TXT' || $filetype2 == 'txt' || $filetype2 == 'TXT')
{
require_once('ClassToCompareFiles.php');
$compareFiles = new ClassToCompareFiles;
// File paths of the two files
@$file1 = $_FILES['mainFile']['tmp_name'];
@$file2 = $_FILES['fileToCompare']['tmp_name'];
@$file1Contents = file($file1);
@$file2Contents = file($file2);
$compareFiles->compareFiles($file1, $file2);
}else{
exit("<script>alert('Please check File Type');history.back();</script>");
}
?>
<?php
echo "<center><font face='verdana' size='2' color='green'><b>Number of Similar line (s): ". $compareFiles->cnt1."</font>";
echo " ";
echo "<font face='verdana' size='2' color='red'>Number of Different line (s): ". $compareFiles->cnt2."</font></center></b>";
?>
<table border="1" style="width:100%;height:400px" cellspacing="0" cellpadding="0">
<tr>
<td bgcolor="#ccddff" style="width:50%" >
<iframe src="file1.html" width="100%" height="650" frameborder='0' ></iframe>
</td>
<td bgcolor="#ffccdd" style="width:50%" >
<iframe src="file2.html" width="100%" height="650" frameborder='0' ></iframe>
</td>
</tr>
</table>
<?php
}
?>
</body>
</html>
ClassToCompareFiles.php
Code (PHP)
<?Php
/**
* Class to compare two similar (text) files
* It returns the output in two iframes and highlights the differences
*
*
* @author Rochak Chauhan
*/
Class ClassToCompareFiles {
public $cnt1 = 0;
public $cnt2 = 0;
/**
* Function to create an array from file contents (text)
*
* @param $fileContents string - File contents
*
* @return array - array of lines of the file contents
*/
private function filetoArray($fileContents) {
return file($fileContents);
}
/**
* Function to compare two file contents
*
* @param $file1 string - path of the first file
* @param $file2 string - path of the second file
*
*/
public function compareFiles($file1, $file2) {
$file1ReturnArray = Array();
$file2ReturnArray = Array();
$mainFileArray = file($file1);
$duplicateFileArray = file($file2);
$linesInMainFile = count($mainFileArray);
$linesInDuplicateFile = count($duplicateFileArray);
// Main login to highlight lines
for($i=0; $i<$linesInDuplicateFile; $i++) {
if($mainFileArray[$i] == $duplicateFileArray[$i]) {
$file1ReturnArray[] = "<font style='background-color:#ccddff'>".htmlentities($mainFileArray[$i])."</font>";
$file2ReturnArray[] = "<font style='background-color:#ffccdd'>".htmlentities($duplicateFileArray[$i])."</font>";
$this->cnt1++;
}
else{
$file1ReturnArray[] = "<font style='background-color:#ffccdd'>".htmlentities($mainFileArray[$i])."</font>";
$file2ReturnArray[] = "<font style='background-color:#ccddff'>".htmlentities($duplicateFileArray[$i])."</font>";
$this->cnt2++;
}
}
$this->createIFrames($file1ReturnArray, $file2ReturnArray);
}
/**
* Function to create left and right iframes to display the comparison between files.
*
* @param $file1Contents array - Modified/ Highlighted contents of file1
* @param $file2Contents array - Modified/ Highlighted contents of file2
*
*/
public function createIFrames($file1Contents, $file2Contents) {
// prepare file1 to display
$openFile1 = fopen('file1.html', 'w');
$html1Code = "<html> <body bgcolor='#ccddff'>";
fwrite($openFile1, $html1Code);
for($i=0; $i<count($file1Contents); $i++) {
$file1 = "<PRE><font style='background-color:#FFFFFF'><b>".($i+1). "</b></font>".$file1Contents[$i]."</PRE>";
fwrite($openFile1, $file1);
}
fwrite($openFile1, "</body></html>");
fclose($openFile1);
// prepare file2 to display
$openFile2 = fopen('file2.html', 'w');
$html2Code = "<html> <body bgcolor='#ffccdd'>";
fwrite($openFile2, $html2Code);
for($j=0; $j<count($file2Contents); $j++) {
$file2 = "<PRE><font style='background-color:#FFFFFF'><b>".($j+1). "</b></font>".$file2Contents[$j]."</PRE>";
fwrite($openFile2, $file2);
}
fwrite($openFile2, "</body></html>");
fclose($openFile2);
}
}
?>
ตัวอย่าง

Tag : PHP, HTML, CSS, HTML5, CakePHP, FuelPHP

|
ประวัติการแก้ไข 2019-10-16 10:06:25 2019-10-16 10:13:09
|
 |
 |
 |
 |
Date :
2019-10-16 10:05:37 |
By :
kittipongw |
View :
667 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ใช้ online tools ง่ายกว่ามั้ย
|
 |
 |
 |
 |
Date :
2020-08-02 22:57:16 |
By :
PhrayaDev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 03
|