Community Documentation

Calculating diff between two fields

Last updated April 18, 2012. Created by Aren Cambre on May 18, 2009.
Edited by cluther. Log in to edit this page.

This compares the values of two text fields using PEAR.

<?php
$path
= '/pathToPearsParentDirectory/pear/PEAR';
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once
'PEAR.php';
include_once
"Text/Diff.php";
include_once
"Text/Diff/Renderer.php";
include_once
"Text/Diff/Renderer/inline.php";

$diff = &new Text_Diff('auto', array(array($node->field_nameOfOneFieldToCompare[0]['value']), array($node->field_nameOfOtherFieldToCompare[0]['value'])));
$renderer = &new Text_Diff_Renderer_inline();

$node_field[0]['value'] = $renderer->render($diff);
?>

Notes:

  • Remove the <?php and ?> from the code before pasting!
  • You must install PEAR's Text_Diff module. This isn't part of the standard install.
  • The $path and set_include_path lines are unnecessary if your PHP engine's include_path already includes the PEAR root directory. I had to override because I had to install my own copy of PEAR; my web host's version didn't include Text_Diff and was too out of date to allow the install.
  • Until #367281: Longtext field is broken is fixed, you should use varchar or text instead of longtext.

More details at http://arencambre.com/blog/2009/05/17/how-i-got-field-diffs-working-with...

About this page

Drupal version
Drupal 6.x
Audience
Developers and coders, Site administrators

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here