verdrm Posted October 14, 2007 Share Posted October 14, 2007 Does anyone know how to display (preferably highlighted) the difference between two sets of text? If I echo two sets of text from MySQL, is there a way to show a difference? Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/ Share on other sites More sharing options...
kratsg Posted October 14, 2007 Share Posted October 14, 2007 Can you make this clearer? Are you asking to check two see if two pieces of text are the same or different? <?php $var1 = "hello"; $var2 = "goodbye"; $var3 = "hello"; if($var1 == $var2){//this isn't true echo "The first variable does equal the second variable."; } else { echo "The first variable does not equal the second variable."; } if($var1 == $var3){//this is true echo "The first variable does equal the third variable."; } else { echo "The first variable does not equal the third variable."; } ?> In the case above.. You get this output: The first variable does not equal the second variable. The first variable does equal the third variable. Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369372 Share on other sites More sharing options...
verdrm Posted October 14, 2007 Author Share Posted October 14, 2007 How can I show highlighted or colored differences? If I have two paragraphs, and they are both different, how do I show that one is different than the other by highlighting? Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369397 Share on other sites More sharing options...
verdrm Posted October 15, 2007 Author Share Posted October 15, 2007 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369633 Share on other sites More sharing options...
MadTechie Posted October 15, 2007 Share Posted October 15, 2007 probably using http://uk2.php.net/highlight-string and http://uk2.php.net/manual/en/function.substr-compare.php Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369638 Share on other sites More sharing options...
verdrm Posted October 15, 2007 Author Share Posted October 15, 2007 Does anyone know how to tell the difference between two sets of text. This needs to be compatible with PHP4. I wrote a little something but it is missing the way to tell the difference. I would really like this to either COLOR, HIGHLIGHT, or BOLD the different text. <?php $ted='echo 123'; $fred='echo 1234'; echo $ted; echo $fred; if($ted !== $fred){ echo'';} ?> Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369705 Share on other sites More sharing options...
irkevin Posted October 15, 2007 Share Posted October 15, 2007 I'm not that good at php, just started. if($ted !== $fred) instead of using !== , why don't u simply use != ??? Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369732 Share on other sites More sharing options...
MasterACE14 Posted October 15, 2007 Share Posted October 15, 2007 try this: <?php $ted = 'echo 123'; $fred = 'echo 1234'; echo "" . $ted . "<br>"; echo "" . $fred . "<br>"; if($ted !== $fred){ echo "<b> ted does not equal fred</b>"; } ?> or echo $fred, and you can use HTML color tags between the double comma's. I'm not that good at php, just started. if($ted !== $fred) instead of using !== , why don't u simply use != ??? you are better off using 2 equal signs to get a more absolute answer. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369734 Share on other sites More sharing options...
irkevin Posted October 15, 2007 Share Posted October 15, 2007 Thanks for the tip MasterACE14, should remember that Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369735 Share on other sites More sharing options...
MasterACE14 Posted October 15, 2007 Share Posted October 15, 2007 no problem Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369737 Share on other sites More sharing options...
trq Posted October 15, 2007 Share Posted October 15, 2007 This function will show (bold) the difference between the $target string and the $compare string. Note it will only bold the different characters it finds in $compare. If you want to highlight the difference both ways it will need a fair bit more work, but this will get you a start. <?php $ted='this is text'; $fred='this is more text'; function highlight_diff($target,$comparison) { $tmp = ''; for ($i = 0; $i <= strlen($target)-1; $i++) { if (strlen($comparison) <= $i) { return $tmp . substr($target,strlen($comparison)); } if ($target{$i} != " ") { if ($target{$i} == $comparison{$i}) { $tmp .= $comparison{$i}; } else { $tmp .= "<b>" . $comparison{$i} . "</b>"; } } else { $tmp .= " "; } } if (strlen($comparison) > strlen($target)) { $tmp .= "<b>" . substr($comparison,strlen($target)) . "</b>"; } return $tmp; } echo highlight_diff($ted,$fred); ?> Be warned, I only just quickly wrote this so its probably riddled with bugs. Quote Link to comment https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369747 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.