Forum Moderators: coopster

Message Too Old, No Replies

PHP compare MySQL entrys

         

CodilX

5:22 pm on Mar 22, 2008 (gmt 0)

10+ Year Member



Hi :)

I'm wondering if someone could help me out.

I'm trying to write a PHP script to compare MySQL entrys and bold/red out the differences in PHP..

I'll try to explain:

¦------------------------------------------
¦ database 1:
¦ id¦ string
¦ -----------------------------------------
¦ 1 ¦ I like to eat meat with potatoes
¦
¦------------------------------------------
¦
¦ database 2:
¦ id¦ string
¦ ------------------------------------------
¦ 1¦ I like to eat meat, and fish, with potatoes
¦------------------------------------------

Output after PHP/MySQL:

¦
¦ I like to eat meat, and fish, with potatoes
¦

I can only manage to compare if they strings in both databases are the same, but I can't manage to get the differences shown the way I want..

I hope someone understood what I want, and I hope someone could help me :) thanks

eelixduppy

2:29 pm on Mar 23, 2008 (gmt 0)



I threw this together real quick, but it should do what you want:


$str1 = 'I like to eat meat with potatoes';
$str2 = 'I like to eat test and meat, and fish, with potatoes and a nice drink potatoes ';
#
function diff($small, $large, $start, $end) {
$small .= ' ';
$diff = '';
$i = 0;
$j = 0;
$on = false;
for($i; $i < strlen($large); $i++) {
if($large[$i] == $small[$j]) {
if($on)
$diff .= $end.$large[$i];
else
$diff .= $large[$i];
$on = false;
#
if(isset($small[$j+1]))
$j++;
} else {
if(!$on)
$diff .= $start.$large[$i];
else
$diff .= $large[$i];
$on = true;
}
#
}
#
return trim($diff);
}
#
echo diff($str1, $str2, '<font size="+4">' , '</font>');

CodilX

2:42 pm on Mar 23, 2008 (gmt 0)

10+ Year Member



thank you so much, it works perfectly! thank you!