欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

使用php比较文本内容的差异

程序员文章站 2022-06-12 21:00:31
...

这个类可以查找和查看文本字符串之间的差异。 它有两个文本字符串和使用的差异算法来找出它们之间的差异,并返回一个列表的变化,来修补原来的字符串成为最后的字符串。 PHP 源码与演示: 源码出处演示出处 ?php/* *from:http://www.codepearl.com */require(

这个类可以查找和查看文本字符串之间的差异。

它有两个文本字符串和使用的差异算法来找出它们之间的差异,并返回一个列表的变化,来修补原来的字符串成为最后的字符串。 PHP

源码与演示:源码出处 演示出处

使用php比较文本内容的差异
Test the Diff Object


by
mode = $mode; if($diff->FormatDiffAsHtml($before, $after, $difference)) echo '
Difference
', $difference->html, '
'; else echo '
Error: ', HtmlSpecialChars($diff->error), '
'; } ?>
http://www.codepearl.com@(#) $Id: diff.php,v 1.4 2013/11/06 21:19:07 mlemos Exp $Copyright ?(C) http://www.codepearl.comOAuth client
Manuel Lemoshttp://www.codepearl.comen..

{/metadocument}
*/

class diff_class
{
	var $error = '';
	var $insertedStyle = 'font-weight: bold';
	var $deletedStyle = 'text-decoration: line-through';

	Function SplitString($string, $separators, $end, &$positions)
	{
		$l = strlen($string);
		$split = array();
		for($p = 0; $p 
		DiffBOOLEAN..beforeSTRING.afterSTRING.differenceHASH.
{/metadocument}
*/
	Function Diff($before, $after, &$difference)
	{
		$mode = (IsSet($difference->mode) ? $difference->mode : 'c');
		switch($mode)
		{
			case 'c':
				$lb = strlen($before);
				$la = strlen($after);
				break;

			case 'w':
				$before = $this->SplitString($before, " \t", "\r\n", $posb);
				$lb = count($before);
				$after = $this->SplitString($after, " \t", "\r\n", $posa);
				$la = count($after);
				break;

			case 'l':
				$before = $this->SplitString($before, "\r\n", '', $posb);
				$lb = count($before);
				$after = $this->SplitString($after, "\r\n", '', $posa);
				$la = count($after);
				break;

			default:
				$this->error = $mode.' is not a supported more for getting the text differences';
				return false;
		}
		$diff = array();
		for($b = $a = 0; $b '=',
					'position'=>($mode === 'c'  ? $b : $posb[$b]),
					'length'=>($mode === 'c' ? $pb - $b : $posb[$pb] - $posb[$b])
				);
				$b = $pb;
			}
			if($b === $lb)
				break;
			for($pb = $b; $pb '-',
					'position'=>($mode === 'c'  ? $b : $posb[$b]),
					'length'=>($mode === 'c' ? $pb - $b : $posb[$pb] - $posb[$b])
				);
				$b = $pb;
			}
			if($pa !== $a)
			{
				$diff[] = array(
					'change'=>'+',
					'position'=>($mode === 'c'  ? $a : $posa[$a]),
					'length'=>($mode === 'c' ? $pa - $a : $posa[$pa] - $posa[$a])
				);
				$a = $pa;
			}
		}
		if($a '+',
				'position'=>($mode === 'c'  ? $a : $posa[$a]),
				'length'=>($mode === 'c' ? $la - $a : $posa[$la] - $posa[$a])
			);
		}
		$difference->difference = $diff;
		return true;
	}
/*
{metadocument}
		
{/metadocument}
*/

	Function FormatDiffAsHtml($before, $after, &$difference)
	{
		if(!$this->diff($before, $after, $difference))
		{
			return false;
		}
		$html = '';
		$insertedStyle = (strlen($this->insertedStyle) ? ' style="'.HtmlSpecialChars($this->insertedStyle).'"' : '');
		$deletedStyle = (strlen($this->deletedStyle) ? ' style="'.HtmlSpecialChars($this->deletedStyle).'"' : '');
		$td = count($difference->difference);
		for($d = 0; $d difference[$d];
			switch($diff['change'])
			{
				case '=':
					$html .= nl2br(HtmlSpecialChars(substr($before, $diff['position'], $diff['length'])));
					break;
				case '-':
					$html .= ''.nl2br(HtmlSpecialChars(substr($before, $diff['position'], $diff['length']))).'';
					break;
				case '+':
					$html .= '';
					break;
				default:
					$this->error = $diff['change'].' is not an expected difference change type';
					return false;
			}
		}
		$difference->html = $html;
		return true;
	}
};

/*

{metadocument}

{/metadocument}

*/

?>
使用php比较文本内容的差异