I appreciate the effort. Yours is great in that it takes length into account. But what if the strings are two different lengths like this?
$str1 = 'page.php?arg1=one&arg2=two&arg3=three';
$str = 'page.php?arg1=one&argx=X&arg3=three';
Despite the fact that arg3 is the same in both strings, it will be flagged as different.
I tried another approach which does very little too:
<?php
$str1 = '123456789a1234567890b123456789';
$str2 = '12345678901234c234567890123456';
//Call the function on the two strings
highlight_different($str1,$str2);
function highlight_different($str1, $str2) {
//Load an array with all values of $str1 as indivudal values
$s1 = str_split($str1);
//Load an array with all values of $str2 as indivudal values
$s2 = str_split($str2);
$only1 = array_diff($s1, $s2);
$only2 = array_diff($s2, $s1);
$len1 = count($s1);
echo "string 1 ($len1): ";
for($i=0; $i<$len1; $i++) {
$c = $s1[$i];
if (in_array($c, $only1)) {
// make it blue
echo '<span style="color:#0000ff;">' . $c . '</span>';
} elseif (in_array($c, $only2)) {
// make it orange
echo '<span style="color:#ffdd00;">' . $c . '</span>';
} else {
echo $c;
}
}
echo '<br>';
$len2 = count($s2);
echo "string 2 ($len2): ";
for($i=0; $i<$len2; $i++) {
$c = $s2[$i];
if (in_array($c, $only2)) {
// make it blue
echo '<span style="color:#0000ff;">' . $c . '</span>';
} elseif (in_array($c, $only1)) {
// make it orange
echo '<span style="color:#ffdd00;">' . $c . '</span>';
} else {
echo $c;
}
}
echo '<br>';
}
?>
Just try it on this to watch it fail:
$str1 = 'http://www.phpbuilder.com/board/showthread.php?p=10909748#post10909748';
$str2 = 'http://www.phpbuilder.com/board/showthread.php?p=10909874#post10909874';
This is what I mean, the diff algorithm is pretty tricky.
I looked on wikipedia and found the 'Longest Common Subsequence Problem'
I think that's what I'm after.