0

I want to highlight sequentially different between 2 string word in PHP, suppose I have 2 String like below.

$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";

The highlighted words should be Arab To -092541 and KDS. so far I have tried the below code.

<?php
function compare_strings($array1, $array2) {
$array1 = explode(" ", $array1);
$array2 = explode(" ", $array2);
$arSize1=count($array1);
$arSize2=count($array2);
$index=0;
  if($arSize1<$arSize2){
    for($i=0;$i<$arSize1;$i++){
      if($array1[$i]==$array2[($i+$index)]){
          echo ' '.$array1[$i];
      }
      else{
          echo ' <b>'.$array2[($i+$index)].'</b>'; 
          $index++;
          $i--;
      }
    }

  }
}
$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/>';
compare_strings($string1,$string2);
?>

this code works fine if I only add words, like Arab To and KDS and don't change existing word -092541. when I change -09254 to -092541 it's start a infinite loop and shows Undefined array key error.

Expected highlighted words are Arab TO -092541 KDS

I tried below answers/libraries but none of these worked for my situation.

@Arif 's answer for a similar question
Side-by-side
class.Diff.php

Any help/guidance will be very appreciated.

Mahmood Sanjrani
  • 108
  • 3
  • 19

2 Answers2

0

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...:)

Shlomtzion
  • 674
  • 5
  • 12
0

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

Mahmood Sanjrani
  • 108
  • 3
  • 19