使ç¨phpæ¯è¾ææ¬å 容çå·®å¼
程序员文章站
2022-06-12 21:00:31
...
这个类可以查找和查看文本字符串之间的差异。 它有两个文本字符串和使用的差异算法来找出它们之间的差异,并返回一个列表的变化,来修补原来的字符串成为最后的字符串。 PHP 源码与演示: 源码出处演示出处 ?php/* *from:http://www.codepearl.com */require(
这个类可以查找和查看文本字符串之间的差异。
它有两个文本字符串和使用的差异算法来找出它们之间的差异,并返回一个列表的变化,来修补原来的字符串成为最后的字符串。 PHP
源码与演示:源码出处 演示出处
Test the Diff Object
{/metadocument} */ ?> http://www.codepearl.com @(#) $Id: diff.php,v 1.4 2013/11/06 21:19:07 mlemos Exp $ Copyright ?(C) http://www.codepearl.com OAuth client Manuel Lemos http://www.codepearl.com {/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 en . . Diff BOOLEAN . . before STRING . after STRING . difference HASH . {/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}
上一篇: 在PHP下 给MYSQL变量报错
下一篇: Oracle 11g alert文件变化