Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > ติดตรง Compare ไฟล์ txt แล้ว Highlight ตรงตัวอักษรคำที่ไม่เหมือนกันไม่ได้ค่ะ



 

ติดตรง Compare ไฟล์ txt แล้ว Highlight ตรงตัวอักษรคำที่ไม่เหมือนกันไม่ได้ค่ะ

 



Topic : 134346



โพสกระทู้ ( 56 )
บทความ ( 0 )



สถานะออฟไลน์




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()"/>&nbsp;&nbsp;&nbsp;
					<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 "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	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);

	}
}
?>



ตัวอย่าง
fsefseffgf



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









ประวัติการแก้ไข
2019-10-16 10:06:25
2019-10-16 10:13:09
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2019-10-16 10:05:37 By : kittipongw View : 575 Reply : 1
 

 

No. 1



โพสกระทู้ ( 1,458 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Blogger

ใช้ online tools ง่ายกว่ามั้ย






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2020-08-02 22:57:16 By : PhrayaDev
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ติดตรง Compare ไฟล์ txt แล้ว Highlight ตรงตัวอักษรคำที่ไม่เหมือนกันไม่ได้ค่ะ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่