ホームページ php教程 php手册 使用php比较文本内容的差异

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

Jun 06, 2016 pm 07:32 PM
php 使用 コンテンツ できる 違い 文章 探す チェック 比較する これ

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

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

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

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

使用php比较文本内容的差异
<?php
/*
 *from:http://www.codepearl.com
 */
	require('diff.php');
 
 $before = IsSet($_POST['before']) ? $_POST['before'] : 'Some text before';
 $after = IsSet($_POST['after']) ? $_POST['after'] : 'This is the text after';
 $mode = (IsSet($_POST['mode']) ? $_POST['mode'] : 'w');
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="en">
<head>
<title>Test the Diff Object</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<style type="text/css">
* { font-family: sans-serif,arial,helvetica }
</style>
<body>
<form method="POST" action="?">
<div><label for="before">Before</label><br>
<textarea id="before" cols="80" rows="10" name="before"><?php echo HtmlSpecialChars($before); ?></textarea></div>
<div><label for="after">After</label><br>
<textarea id="after" cols="80" rows="10" name="after"><?php echo HtmlSpecialChars($after); ?></textarea></div>
<div><input type="submit" name="compare" value="Compare"> by <select name="mode">
<option value="c"<?php if($mode === 'c') echo ' selected'; ?>>Character</option>
<option value="w"<?php if($mode === 'w') echo ' selected'; ?>>Word</option>
<option value="l"<?php if($mode === 'l') echo ' selected'; ?>>Line</option>
</select></div>
<?php
	if(IsSet($_POST['compare']))
	{
		$diff = new diff_class;
		$difference = new stdClass;
		$difference->mode = $mode;
		if($diff->FormatDiffAsHtml($before, $after, $difference))
			echo '<div>Difference<br><div style="border-style: solid; border-width: 1px">', $difference->html, '</div></div>';
		else
			echo '<div>Error: ', HtmlSpecialChars($diff->error), '</div>';
	}
?>
</form>
</body>
</html>
ログイン後にコピー
<?php
/*
 *from:http://www.codepearl.com
 */

/*
{metadocument}<?xml version="1.0" encoding="ISO-8859-1" ?>
<class>

	<package>http://www.codepearl.com</package>

	<version>@(#) $Id: diff.php,v 1.4 2013/11/06 21:19:07 mlemos Exp $</version>
	<copyright>Copyright ?(C) http://www.codepearl.com</copyright>
	<title>OAuth client</title>
	<author>Manuel Lemos</author>
	<authoraddress>http://www.codepearl.com</authoraddress>

	<documentation>
		<idiom>en</idiom>
		<purpose>.</purpose>
		<usage>.</usage>
	</documentation>

{/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 < $l;)
		{
			$e = strcspn($string, $separators.$end, $p);
			$e += strspn($string, $separators, $p + $e);
			$split[] = substr($string, $p, $e);
			$positions[] = $p;
			$p += $e;
			if(strlen($end)
			&& ($e = strspn($string, $end, $p)))
			{
				$split[] = substr($string, $p, $e);
				$positions[] = $p;
				$p += $e;
			}
		}
		$positions[] = $p;
		return $split;
	}

/*
{metadocument}
	<function>
		<name>Diff</name>
		<type>BOOLEAN</type>
		<documentation>
			<purpose>.</purpose>
			<usage>.</usage>
		</documentation>
		<argument>
			<name>before</name>
			<type>STRING</type>
			<documentation>
				<purpose>.</purpose>
			</documentation>
		</argument>
		<argument>
			<name>after</name>
			<type>STRING</type>
			<documentation>
				<purpose>.</purpose>
			</documentation>
		</argument>
		<argument>
			<name>difference</name>
			<type>HASH</type>
			<out />
			<documentation>
				<purpose>.</purpose>
			</documentation>
		</argument>
		<do>
{/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 < $lb && $a < $la;)
		{
			for($pb = $b; $a < $la && $pb < $lb && $after[$a] === $before[$pb]; ++$a, ++$pb);
			if($pb !== $b)
			{
				$diff[] = array(
					'change'=>'=',
					'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 < $lb; ++$pb)
			{
				for($pa = $a ; $pa < $la && $after[$pa] !== $before[$pb]; ++$pa);
				if($pa !== $la)
					break;
			}
			if($pb !== $b)
			{
				$diff[] = array(
					'change'=>'-',
					'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 < $la)
		{
			$diff[] = array(
				'change'=>'+',
				'position'=>($mode === 'c'  ? $a : $posa[$a]),
				'length'=>($mode === 'c' ? $la - $a : $posa[$la] - $posa[$a])
			);
		}
		$difference->difference = $diff;
		return true;
	}
/*
{metadocument}
		</do>
	</function>
{/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 < $td; ++$d)
		{
			$diff = $difference->difference[$d];
			switch($diff['change'])
			{
				case '=':
					$html .= nl2br(HtmlSpecialChars(substr($before, $diff['position'], $diff['length'])));
					break;
				case '-':
					$html .= '<del'.$deletedStyle.'>'.nl2br(HtmlSpecialChars(substr($before, $diff['position'], $diff['length']))).'</del>';
					break;
				case '+':
					$html .= '<ins'.$insertedStyle.'>'.nl2br(HtmlSpecialChars(substr($after, $diff['position'], $diff['length']))).'</ins>';
					break;
				default:
					$this->error = $diff['change'].' is not an expected difference change type';
					return false;
			}
		}
		$difference->html = $html;
		return true;
	}
};

/*

{metadocument}
</class>
{/metadocument}

*/

?>
ログイン後にコピー
使用php比较文本内容的差异
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

CakePHP プロジェクトの構成 CakePHP プロジェクトの構成 Sep 10, 2024 pm 05:25 PM

この章では、CakePHP の環境変数、一般設定、データベース設定、電子メール設定について理解します。

Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Ubuntu および Debian 用の PHP 8.4 インストールおよびアップグレード ガイド Dec 24, 2024 pm 04:42 PM

PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

CakePHP の日付と時刻 CakePHP の日付と時刻 Sep 10, 2024 pm 05:27 PM

Cakephp4 で日付と時刻を操作するには、利用可能な FrozenTime クラスを利用します。

CakePHP ファイルのアップロード CakePHP ファイルのアップロード Sep 10, 2024 pm 05:27 PM

ファイルのアップロードを行うには、フォーム ヘルパーを使用します。ここではファイルアップロードの例を示します。

CakePHP ルーティング CakePHP ルーティング Sep 10, 2024 pm 05:25 PM

この章では、ルーティングに関連する次のトピックを学習します。

CakePHP について話し合う CakePHP について話し合う Sep 10, 2024 pm 05:28 PM

CakePHP は、PHP 用のオープンソース フレームワークです。これは、アプリケーションの開発、展開、保守をより簡単にすることを目的としています。 CakePHP は、強力かつ理解しやすい MVC のようなアーキテクチャに基づいています。モデル、ビュー、コントローラー

PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 PHP 開発用に Visual Studio Code (VS Code) をセットアップする方法 Dec 20, 2024 am 11:31 AM

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

CakePHP バリデータの作成 CakePHP バリデータの作成 Sep 10, 2024 pm 05:26 PM

Validator は、コントローラーに次の 2 行を追加することで作成できます。

See all articles