Use php to compare differences in text content

Source: Internet
Author: User
This class can be used to find and view the differences between text strings. It has two text strings and the difference algorithm used to identify the differences between them, and returns a list change to fix the original string as the final string. PHP source code and Demonstration: source code demonstration? Php ** from: www.codepearl.com * require (

This class can be used to find and view the differences between text strings. It has two text strings and the difference algorithm used to identify the differences between them, and returns a list change to fix the original string as the final string. PHP source code and Demonstration: source code demonstration? Php/** from: http://www.codepearl.com */require (

This class can be used to find and view the differences between text strings.

It has two text strings and the difference algorithm used to identify the differences between them, and returns a list change to fix the original string as the final string. PHP

Source code and demo:Source code source demonstration Source

 Test the Diff Object
 
 
 
  
   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 clientManuel Lemoshttp://www.codepearl.com
  
   
    en
   
   
    .
   
   
    .
   
  {/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}
  
   
    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 < $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}
   
  {/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 .= '
  
   '.nl2br(HtmlSpecialChars(substr($before, $diff['position'], $diff['length']))).'';break;case '+':$html .= '
   
    '.nl2br(HtmlSpecialChars(substr($after, $diff['position'], $diff['length']))).'';break;default:$this->error = $diff['change'].' is not an expected difference change type';return false;}}$difference->html = $html;return true;}};/*{metadocument}
   
  
 {/metadocument}*/?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.