function diffChar($orig, $final, $words = 0, $function = 'character') { if ($words) { preg_match_all("/\\w+\\s+(?=\\w)|\\w+|\\W/", implode("<br />", $orig), $matches); $line1 = $matches[0]; preg_match_all("/\\w+\\s+(?=\\w)|\\w+|\\W/", implode("<br />", $final), $matches); $line2 = $matches[0]; } else { $line1 = preg_split('//', implode("<br />", $orig), -1, PREG_SPLIT_NO_EMPTY); $line2 = preg_split('//', implode("<br />", $final), -1, PREG_SPLIT_NO_EMPTY); } $z = new Text_Diff($line1, $line2); if ($z->isEmpty()) { return array($orig[0], $final[0]); } //echo "<pre>";print_r($z);echo "</pre>"; require_once "renderer_{$function}.php"; $new = "Text_Diff_Renderer_{$function}"; $renderer = new $new(sizeof($line1)); return $renderer->render($z); }
function diffChar($orig, $final) { $line1 = preg_split('//', implode("<br />", $orig), -1, PREG_SPLIT_NO_EMPTY); $line2 = preg_split('//', implode("<br />", $final), -1, PREG_SPLIT_NO_EMPTY); $z = new Text_Diff($line1, $line2); if ($z->isEmpty()) { return array($orig[0], $final[0]); } // echo "<pre>";print_r($z);echo "</pre>"; require_once 'character.php'; $renderer = new Text_Diff_Renderer_character(10000); return $renderer->render($z); }
function diffChar($orig, $final, $words = 0, $function = 'character') { $glue = strpos($function, 'inline') !== false ? "<br />" : "\n"; if ($words) { preg_match_all("/\\w+\\s+(?=\\w)|\\w+|\\W/u", implode($glue, $orig), $matches); $line1 = $matches[0]; preg_match_all("/\\w+\\s+(?=\\w)|\\w+|\\W/u", implode($glue, $final), $matches); $line2 = $matches[0]; } else { $line1 = preg_split('//u', implode($glue, $orig), -1, PREG_SPLIT_NO_EMPTY); $line2 = preg_split('//u', implode($glue, $final), -1, PREG_SPLIT_NO_EMPTY); } $z = new Text_Diff($line1, $line2); if ($z->isEmpty()) { return array($orig[0], $final[0]); } //echo "<pre>";print_r($z);echo "</pre>"; compileRendererClass($function); $new = "Text_Diff_Renderer_{$function}"; $renderer = new $new(count($line1)); return $renderer->render($z); }
/** * Get a html diff between two versions. * * @param string latest_revision id of the latest revision * @param string oldest_revision id of the oldest revision * @return array array with the original value, the new value and a diff -u */ public function get_diff($oldest_revision, $latest_revision, $renderer_style = 'inline') { if (!class_exists('Text_Diff')) { @(include_once 'Text/Diff.php'); @(include_once 'Text/Diff/Renderer.php'); @(include_once 'Text/Diff/Renderer/unified.php'); @(include_once 'Text/Diff/Renderer/inline.php'); if (!class_exists('Text_Diff')) { throw new midcom_error("Failed to load Text_Diff library."); } } $oldest = $this->get_revision($oldest_revision); $newest = $this->get_revision($latest_revision); $return = array(); foreach ($oldest as $attribute => $oldest_value) { if (!array_key_exists($attribute, $newest)) { continue; // This isn't in the newer version, skip } if (is_array($oldest_value)) { continue; // Skip } $return[$attribute] = array('old' => $oldest_value, 'new' => $newest[$attribute]); if ($oldest_value != $newest[$attribute]) { $lines1 = explode("\n", $oldest_value); $lines2 = explode("\n", $newest[$attribute]); $diff = new Text_Diff($lines1, $lines2); if ($renderer_style == 'unified') { $renderer = new Text_Diff_Renderer_unified(); } else { $renderer = new Text_Diff_Renderer_inline(); } if (!$diff->isEmpty()) { // Run the diff $return[$attribute]['diff'] = $renderer->render($diff); if ($renderer_style == 'inline') { // Modify the output for nicer rendering $return[$attribute]['diff'] = str_replace('<del>', "<span class=\"deleted\" title=\"removed in {$latest_revision}\">", $return[$attribute]['diff']); $return[$attribute]['diff'] = str_replace('</del>', '</span>', $return[$attribute]['diff']); $return[$attribute]['diff'] = str_replace('<ins>', "<span class=\"inserted\" title=\"added in {$latest_revision}\">", $return[$attribute]['diff']); $return[$attribute]['diff'] = str_replace('</ins>', '</span>', $return[$attribute]['diff']); } } } } return $return; }