【问题标题】:Horde_Text_Diff string comparison library only comparing 1st character of stringsHorde_Text_Diff 字符串比较库仅比较字符串的第一个字符
【发布时间】:2013-08-08 00:29:37
【问题描述】:

我正在使用 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。换句话说,它似乎只是在比较第一个字符。有什么想法吗?

【问题讨论】:

    标签: text diff horde


    【解决方案1】:

    当我尝试这个时,我收到两个警告:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      相关资源
      最近更新 更多