string matching - highlight sequentially different word in PHP

Solution:

You code is overly complicated ... and that's why you go to infinite loops...

Here is a suggestion with foreach loop, you don't need to care how long you arrays are in this example, since you are checking if the words that come from the array you are looping through appear in the other... so you are already checking all of the words in the array.

This means that if you use something like in_array() you will know if that word appear in the other array or not.

So here is a small change to your code:

<?php
function show_unique_strings($array1, $array2) {

    $array1 = explode(" ", $array1);
    $array2 = explode(" ", $array2);


    foreach ($array2 as $a2Val){

        $nKey = in_array($a2Val,$array1);
        if($nKey > 0){
            echo "<b> $a2Val </b>";
        } else {
            echo " $a2Val";
        }

    }

}

$string1 = "TO THE ORDER OF United Bank Limited Arab -09254 DT:17-06-20212 ADS";
$string2 = "Arab TO THE ORDER OF United Bank Limited Arab TO -092541 KDS DT:17-06-20212";
echo $string1.'<br/>';
show_unique_strings($string1,$string2);
?>

Will output:

TO THE ORDER OF United Bank Limited Arab -09254 DT:17-06-20212 ADS

Arab TO THE ORDER OF United Bank Limited Arab TO -092541 KDS DT:17-06-20212

This way we don't care if we run only on the 2nd array because for each word we do the check and echo one or the other... no need of counts and following our keys at all...:)

Answer

Solution:

simplediff library did my job, I just had to modify code to meet my requirement.

<?php
function diff($old, $new){
    $matrix = array();
    $maxlen = 0;
    foreach($old as $oindex => $ovalue){
        $nkeys = array_keys($new, $ovalue);
        foreach($nkeys as $nindex){
            $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ?
                $matrix[$oindex - 1][$nindex - 1] + 1 : 1;
            if($matrix[$oindex][$nindex] > $maxlen){
                $maxlen = $matrix[$oindex][$nindex];
                $omax = $oindex + 1 - $maxlen;
                $nmax = $nindex + 1 - $maxlen;
            }
        }   
    }
    if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new));
    return array_merge(
        diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)),
        array_slice($new, $nmax, $maxlen),
        diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen)));
}

$string1 = "TO THE ORDER OF United Bank Limited Arab -09254 DT:17-06-20212 ADS";
$string2 = "Arab TO THE ORDER OF United Bank Limited Arab TO -092541 KDS DT:17-06-20212";
$arr=diff(explode(" ",$string1),explode(" ",$string2));
echo $string1.'<br/>';
$ret = '';
foreach($arr as $k){
        if(is_array($k)){
            $ret .= (!empty($k['i'])?"<b>".implode(' ',$k['i'])."</b> ":'');
        }
        else {
          $ret .= $k . ' ';
        }
    }
echo $ret;
?>

output:

TO THE ORDER OF United Bank Limited Arab -09254 DT:17-06-20212 ADS
Arab TO THE ORDER OF United Bank Limited Arab TO -092541 KDS DT:17-06-20212

Source