我正在使用 Horde_Text_Diff 来计算两个字符串之间的差异。示例代码如下:
$check_diff = new Horde_Text_Diff( 'auto', array('asdf','asd11') );
$renderer = new Horde_Text_Diff_Renderer_Inline();
echo $renderer->render($check_diff);
这没有任何回响。正确的行为是在字符 4 处显示差异。
如果我将比较数组从 array('asdf','asd11') 更改为 array('asdf','12345'),那么它将输出 a1。换句话说,似乎只是在比较第一个字符。有什么想法吗?
当我尝试此操作时,我收到两个警告:
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
即,某些东西正在获取它需要数组的字符串。
这是因为,您应该传递(包含)两个字符串数组(其中每个字符串代表一行文本),而不是将(包含)两个字符串的数组传递给 Horde_Text_Diff()。
如果您当前尝试传入的实际字符串包含多行文本,那么您可以使用explode()将它们拆分为字符串数组,例如:
$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);
输出:
foo
<del>bar</del><ins>qux</ins>
baz