How to highlight changes/difference in one text paragraph from the other?

后端 未结 4 999
你的背包
你的背包 2021-01-21 04:03

Is it possible to highlight the modifications in one text paragraph from the other?

For example, there are 3 text fields in a database. Non-admin users can edit the text

4条回答
  •  情深已故
    2021-01-21 04:41

    Not sure, why these long solutions are there. here I had found an easy one for me.

    string1 = "The quick brown fox jumps over the lazy dog.";
    $string2 = "The quick brown albino fox jumps the groovy dog.";
    
    $string1 = explode(" ", $string1);
    $string2 = explode(" ", $string2);
    
    $diff = array_intersect($string2, $string1);
    
    $tmp = array();
    foreach ($string2 as $k => $w) {
             if ($diff[$k]==$w) {
                 $tmp[$k] = $w;
             }
             else {
                   $tmp[$k] = "$w";
             }
    }
    $diff = array_diff($string1, $tmp);
    
    foreach ($diff as $k => $w) {
             $tmp[$k] .= " [$w]";
    }
    
    echo join (' ', $tmp);
    

    ref. https://forums.phpfreaks.com/topic/6525-how-do-i-highlight-differences-between-strings/

提交回复
热议问题