00001 <?php
00008 abstract class SStringHelper extends SObject {
00009
00010
00011
00021 protected static function diffHelper($old, $new){
00022 $maxlen = 0;
00023 foreach($old as $oindex => $ovalue){
00024 $nkeys = array_keys($new, $ovalue);
00025 foreach($nkeys as $nindex){
00026 $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
00027 $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
00028 if($matrix[$oindex][$nindex] > $maxlen){
00029 $maxlen = $matrix[$oindex][$nindex];
00030 $omax = $oindex + 1 - $maxlen;
00031 $nmax = $nindex + 1 - $maxlen;
00032 }
00033 }
00034 }
00035 if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
00036 return array_merge(
00037 self::diffHelper(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
00038 array_slice($new, $nmax, $maxlen),
00039 self::diffHelper(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
00040 }
00050 public static function diff($old, $new){
00051 $diff = self::diffHelper(explode(' ', $old), explode(' ', $new));
00052 $ret = '';
00053 foreach($diff as $k){
00054 if(is_array($k))
00055 $ret .= (!empty($k['d'])?"<del>".implode(' ',$k['d'])."</del> ":'').
00056 (!empty($k['i'])?"<ins>".implode(' ',$k['i'])."</ins> ":'');
00057 else $ret .= $k . ' ';
00058 }
00059 return $ret;
00060 }
00069 public function toHTML($string) {
00070 $string = str_replace('\r', '<br />', $string);
00071 $string = str_replace('\n', '<br />', $string);
00072 $string = str_replace('
00073 ', '<br />', $string);
00074 return $string;
00075 }
00076 }