Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

Calculating diff between two fields

Last updated on
30 April 2025

This compares the values of two text fields using PEAR.


$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...

Help improve this page

Page status: Not set

You can: