PluginProbe
WPMobile.App – Android and iOS App Builder / trunk
WPMobile.App – Android and iOS App Builder vtrunk
wpappninja / debug / diff.php

diff.php in WPMobile.App – Android and iOS App Builder trunk, at debug/diff.php

64 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' );
3
4 /*
5 Paul's Simple Diff Algorithm v 0.1
6 (C) Paul Butler 2007 <http://www.paulbutler.org/>
7 May be used and distributed under the zlib/libpng license.
8
9 This code is intended for learning purposes; it was written with short
10 code taking priority over performance. It could be used in a practical
11 application, but there are a few ways it could be optimized.
12
13 Given two arrays, the function diff will return an array of the changes.
14 I won't describe the format of the array, but it will be obvious
15 if you use print_r() on the result of a diff on some test data.
16
17 htmlDiff is a wrapper for the diff command, it takes two strings and
18 returns the differences in HTML. The tags used are <ins> and <del>,
19 which can easily be styled with CSS.
20 */
21 /**
22 * Diff checker
23 *
24 * @since 4.0.1
25 */
26 function wpappninja_diff($old, $new){
27 $old = preg_replace('#<#', '&lt;', $old);
28 $new = preg_replace('#<#', '&lt;', $new);
29 $old = preg_replace('#>#', '&gt;', $old);
30 $new = preg_replace('#>#', '&gt;', $new);
31 $matrix = array();
32 $maxlen = 0;
33 foreach($old as $oindex => $ovalue){
34 $nkeys = array_keys($new, $ovalue);
35 foreach($nkeys as $nindex){
36 $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
37 $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
38 if($matrix[$oindex][$nindex] > $maxlen){
39 $maxlen = $matrix[$oindex][$nindex];
40 $omax = $oindex + 1 - $maxlen;
41 $nmax = $nindex + 1 - $maxlen;
42 }
43 }
44 }
45 if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
46 return array_merge(
47 wpappninja_diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
48 array_slice($new, $nmax, $maxlen),
49 wpappninja_diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
50 }
51 function wpappninja_html_diff($old, $new, $title = ""){
52 if ($old == $new) {return;}
53 $ret = '<style>ins{font-weight:700;color:green;}del{font-weight:700;color:red}</style><br/><br/><hr/><br/><h2>' . $title . '</h2>';
54 $ret .= '<h3>OLD</h3> ' . $old . ' <h3>NEW</h3> ' . $new . ' <h4>DIFF</h4>';
55 $diff = wpappninja_diff(preg_split("/[\s]+/", $old), preg_split("/[\s]+/", $new));
56 foreach($diff as $k){
57 if(is_array($k))
58 $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
59 (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
60 else $ret .= $k . ' ';
61 }
62 return $ret;
63 }
64