Compare Two Vars And Highlight The Differences

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > PHP Programming

Compare Two Vars And Highlight The Differences

Amezis
I need a script that compares 2 variables, and looks for a difference between them. It will then highlight the words that are different, as shown in the image below:

IPB Image

So in the left cell, the yellow text is the text that has been removed OR replaced.

In the right cell, the green text is the text that has replaced some text in the left cell, OR new text.


This script will be used to compare 2 versions of a document stored in the database, to see the difference between each change (all revisions are stored in the database). I know that it's possible with PHP, because Wikipedia, which is coded in PHP, uses it, but I have absolutely no clue on how to do it. Any suggestions?

Note: This was originally posted back here, but that topic seems dead, so I decided to make a new topic.

Reply

hts
CODE

$string1 = "This is my first sentence";
$string2 = "This is my second sentence";
$find = explode(" ",$string1);
foreach($find as $word){
$text = str_replace ($word, "<b>$word</b>", $string2);
$string2=$text;
}
echo $text;


this might be a starting point for you..it has flaws, as you will notice when you`ll run it ..also, it doesn't do exactly what you`ve asked for..well, just try it for yourself and maybe you`ll be able to modify it to fit your needs (ps: differences are bolded wink.gif )

Reply

Amezis
Well, I've never used foreach. And there is an error with your code. I entered these vars:
CODE
$string1 = "test first and test again";
$string2 = "test second and test again";


And this is what it returns:
CODE
<b><b>test</b></b> second <b>and</b> <b><b>test</b></b> <b>again</b>


On the words "test", there are two <b> tags, because the word "test" appears twice, and it actually highlights the words that are the same, not the words that are different.

And there was another bug:
CODE
$string1 = "a variable is here";
$string2 = "a variable is there";

// Output:
<b>a</b> v<b>a</b>ri<b>a</b>ble <b>is</b> t<b>here</b>

So "a" is highlighted inside the word "variable" too.

 

 

 


Reply

hts
well yes, I knew about those errors..I`ve tested this script on my local server and the "a" letter was highlighted inside a word, too..sorry for this..I`ll think on this, as I might need it sometime later tongue.gif...maybe I`ll come up with something better...

Reply

jlhaslip
After you explode the string, before you add the bold tags, make the array contain only unique entries.
I think the function is called array_unique().
Look on the php.net site for confirmation of the function and the details.

Reply

electron
Well after exploding the same why dont you guys use the array_diff_assoc() function or the array_diff() function.
array_diff_assoc() function would be more appropriate though.

QUOTE
array_diff_assoc() returns an array containing all the values from array1 that are not present in any of the other arguments. Note that the keys are used in the comparison unlike array_diff().

CODE
<?php
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>


The above example will output:

CODE
Array
(
   [b] => brown
   [c] => blue
   [0] => red
)


Well hope this will help in atleast finding the difference.
But how will you arrange the thing I have not come up with that as yet.

Reply

truefusion
After hours of trial and error, i've managed to make a function that does almost exactly what you want! However, there's one tiny little bug i have to fix, before i release it. It seems to be duplicating the last word...

EDIT:
CODE
function compare_contrast($var1, $var2){

$offset1 = preg_split('/\s/', $var1, -1, PREG_SPLIT_OFFSET_CAPTURE);
$split1 = preg_split('/\s/', $var1);

//

$offset2 = preg_split('/\s/', $var2, -1, PREG_SPLIT_OFFSET_CAPTURE);
$split2 = preg_split('/\s/', $var2);

//

$diff1 = array_diff_assoc($split1, $split2);

$i = 0;

foreach ($diff1 as $key => $value){
if ($i == 0){
$compare = substr_compare($var1, $offset1[$key][0], $offset1[$key][1], strlen($var1));
$compare = intval("-$compare");
$temp1 = substr_replace($var1, "<b>".$offset1[$key][0]."</b>", $offset1[$key][1], $compare);

$i = $i + 7;
}
else {
$compare = (strlen($var1)+$i) - (($offset1[$key][1]+$i) + strlen($offset1[$key][0]));
$compare = intval("-$compare");
if ($compare < 0){
$temp1 = substr_replace($temp1, "<b>".$offset1[$key][0]."</b>", $offset1[$key][1]+$i, $compare);
}
else {
$temp1 = substr_replace($temp1, "<b>".$offset1[$key][0]."</b>", $offset1[$key][1]+$i);
}

$i = $i + 7;
}
}

//

$diff2 = array_diff_assoc($split2, $split1);

$i = 0;

foreach ($diff2 as $key => $value){
if ($i == 0){
$compare = substr_compare($var2, $offset2[$key][0], $offset2[$key][1], strlen($var2));
$compare = intval("-$compare");
$temp2 = substr_replace($var2, "<b>".$offset2[$key][0]."</b>", $offset2[$key][1], $compare);

$i = $i + 7;
}
else {
$compare = (strlen($var2)+$i) - (($offset2[$key][1]+$i) + strlen($offset2[$key][0]));
$compare = intval("-$compare");
if ($compare < 0){
$temp2 = substr_replace($temp2, "<b>".$offset2[$key][0]."</b>", $offset2[$key][1]+$i, $compare);
}
else {
$temp2 = substr_replace($temp2, "<b>".$offset2[$key][0]."</b>", $offset2[$key][1]+$i);
}

$i = $i + 7;
}
}

$output = array($temp1, $temp2);
return $output;
}

This is the best i could get it as.

If you're using PHP 4, then you'll need this as well:
CODE
if (!function_exists('substr_compare')){
function substr_compare($main_str, $str, $offset, $length = NULL, $case_insensitivity = false){
$offset = (int) $offset;

// Throw a warning because the offset is invalid
if ($offset >= strlen($main_str)){
trigger_error('The start position cannot exceed initial string length.', E_USER_WARNING);
return false;
}

// We are comparing the first n-characters of each string, so let's use the PHP function to do it
if ($offset == 0 && is_int($length) && $case_insensitivity === true){
return strncasecmp($main_str, $str, $length);
}

// Get the substring that we are comparing
if (is_int($length)){
$main_substr = substr($main_str, $offset, $length);
$str_substr = substr($str, 0, $length);
} else {
$main_substr = substr($main_str, $offset);
$str_substr = $str;
}

// Return a case-insensitive comparison of the two strings
if ($case_insensitivity === true){
return strcasecmp($main_substr, $str_substr);
}

// Return a case-sensitive comparison of the two strings
return strcmp($main_substr, $str_substr);
}
}


To output it use this:
CODE
$output = compare_contrast($str1, $str2);
echo $output[0];
echo $output[1];

Reply

Amezis
Thanks a lot Truefusion, but there is still one bug: If a word has been added, it will highlight everything after the word. So the script wouldn't work correctly with this:
CODE
$var1 = 'A web site with information and stuff';
$var2 = 'A web page with nice information and stuff';


page/site will be highlighted correctly, but I added the word "nice" in the second variable, so everything after that word has been highlighted. Any idea on how to fix that?

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Recent Queries:-
  1. php compare two strings highlight differences function - 220.69 hr back. (1)
  2. code to compare two strings and highlight differences in php - 255.96 hr back. (1)
  3. compare two string difference highlight php - 277.28 hr back. (1)
  4. php highlight differences - 310.11 hr back. (1)
  5. php highlight differences string - 475.81 hr back. (1)
  6. php to compare two sentence - 608.55 hr back. (1)
Similar Topics

Keywords : vars highlight differences

  1. Flash + PHP: How can I transfer vars? - (4)
  2. Script Won't Work At Trap17 -- Version Differences? - But works flawlessly in my machine... (12)
    Here is the code: CODE <?php $data_array = array(); $data_array2 =
    array(); $file_name = "menu_data.txt"; $handle =
    @fopen($file_name, "r"); if (!$handle)  {     echo "File
    Handle Not Available For Use"; exit;         } while (($data = fgetcsv(
    $handle, 1000, ",")) !== FALSE) {         if ( (strpos
    ($data[2],"$page=")))     {
                        $data_array[] =  t...
  3. Problem With $http_post_vars - (3)
    I have a piece of code on one server that works and the same piece of code on another server does
    not. The difference is in the PHP verison & the Linux verison The one that works runs PHP4 on Redhat
    Linux. The one that doesn't runs PHP5 on SUSE Linux. Not sure if this is where the issue lies or
    not. I do know that in one case I see an adrress in searchable and in the other case it is null.
    PHP Code: CODE if ($HTTP_POST_VARS[addr]) {     $searchable =
    $HTTP_POST_VARS[addr];     echo "searchable = ".var_dump(...
  4. Php Version Differences? Or Not? - between the trap and XAMPP??? (5)
    The Trap uses php version 4.4.1 and I have downloaded and installed the XAMPP php Local Server
    configuration which includes php version 5.1.1 and mysql and perl etc, but this question relates to
    the php because that is where I am currently having difficulties. This is the script I am having
    problems with locally: http://www.trap17.com/forums/script-build-...nks-t33362.html Both places
    have the same identical files, code and data. Both are folders in the root directory. I have used
    phpinfo function to confirm the server root is what it should be (public_html and htdo...
  5. Quick Question About Session Vars - something I can't find anywhere (1)
    Can you save stuff like classes and arrays in sessionsvariables?...



Looking for compare, vars, highlight, differences

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for compare, vars, highlight, differences

*MORE FROM TRAP17.COM*
advertisement



Compare Two Vars And Highlight The Differences



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE