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
<?phpand?>from the code before pasting! - You must install PEAR's Text_Diff module. This isn't part of the standard install.
- The
$pathandset_include_pathlines 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:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion