Horde_Text_Diff string comparison library only comparing 1st character of strings

499 views Asked by At

I am using Horde_Text_Diff to compute the difference between two strings. Sample code is as follows:

$check_diff = new Horde_Text_Diff( 'auto', array('asdf','asd11') );

$renderer = new Horde_Text_Diff_Renderer_Inline();
echo $renderer->render($check_diff);

This echoes nothing. The correct behaviour would be to show a difference at character 4.

If I change the comparison array from array('asdf','asd11') to, for instance, array('asdf','12345'), then it will output a1. In other words, it seems only to be comparing the first character. Any ideas?

1

There are 1 answers

0
Michael Dyck On BEST ANSWER

When I try this, I get two warnings:

PHP Warning:  array_walk() expects parameter 1 to be array, string given in /usr/share/php/Horde/Text/Diff/Engine/Native.php on line 33
PHP Warning:  array_walk() expects parameter 1 to be array, string given in /usr/share/php/Horde/Text/Diff/Engine/Native.php on line 34

I.e., something is getting strings where it expects arrays.

That's because, rather than passing (an array containing) two strings to Horde_Text_Diff(), you should pass (an array containing) two arrays-of-strings (where each string represents a line of text).

If the actual strings you're currently trying to pass in contain multiple lines of text, then you can split them into arrays-of-strings using explode(), e.g.:

$a = "foo\nbar\nbaz";
$b = "foo\nqux\nbaz";
$a_lines = explode("\n", $a);
$b_lines = explode("\n", $b);

$check_diff = new Horde_Text_Diff( 'auto', array($a_lines, $b_lines) );
$renderer = new Horde_Text_Diff_Renderer_Inline();
echo $renderer->render($check_diff);

which outputs:

foo
<del>bar</del><ins>qux</ins>
baz