2

How can I compare the two large strings of size 50Kb each using php. I want to highlight the differentiating bits.

Muhammad Usman
  • 10,426
  • 22
  • 72
  • 107

3 Answers3

4

Differences between two strings can also be found using XOR:

$s = 'the sky is falling';
$t = 'the pie is failing';
$d = $s ^ $t;

echo $s, "\n";
for ($i = 0, $n = strlen($d); $i != $n; ++$i) {
        echo $d[$i] === "\0" ? ' ' : '#';
}
echo "\n$t\n";

Output:

the sky is falling
    ###      #
the pie is failing

The XOR operation will result in a string that has '\0' where both strings are the same and something not '\0' if they're different. It won't be faster than just comparing both strings character by character, but it'd be useful if you want to just know the first character that's different by using strspn().

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
3

Do you want to output like diff?

Perhaps this is what you want https://github.com/paulgb/simplediff/blob/5bfe1d2a8f967c7901ace50f04ac2d9308ed3169/simplediff.php

ADDED:

Or if you want to highlight every character that is different, you can use a PHP script like this:

for($i=0;$i<strlen($string1);$i++){
    if($string1[$i]!=$string2[$i]){
        echo "Char $i is different ({$string1[$i]}!={$string2[$i]}<br />\n";
    }
}

Perhaps if you can tell us in detail how you would like to compare, or give us some examples, it would be easier for us to decide the answer.

Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
0

A little modification to @Alvin's script:

I tested it in my local server with a 50kb lorem ipsum string, i substituted all "a" to "4" and it highlight them. It runs pretty fast

    <?php
$string1 = "This is a sample text to test a script to highlight the differences between 2 strings, so the second string will be slightly different";
$string2 = "This is 2 s4mple text to test a scr1pt to highlight the differences between 2 strings, so the first string will be slightly different";
    for($i=0;$i<strlen($string1);$i++){                 
        if($string1[$i]!=$string2[$i]){
            $string3[$i] = "<mark>{$string1[$i]}</mark>";
            $string4[$i] = "<mark>{$string2[$i]}</mark>";
        }
        else {
            $string3[$i] = "{$string1[$i]}";
            $string4[$i] = "{$string2[$i]}";    
        }
    }
    $string3 = implode("",$string3);
    $string4 = implode("",$string4);

    echo "$string3". "<br />". $string4;
?>
davidaam
  • 443
  • 1
  • 8
  • 17