Jump to content

see difference between two string


novi

Recommended Posts

I have two variables each holding a string of data. The string may be a simple short sentence of text, it may be several paragraphs of text.

 

Call one "Original", and the other "Modified"

The "Modified" string is a modified version of the the "Original".

 

On my PHP page I need to show both strings side by side, that's no problem! However to aid my users in finding the modifications I want to highlight using a background color or font color where the modified string differs from the original.

 

I was just wondering if anyone had tackled anything like this before?

 

I would really appreciate any help you could give me.

 

Novi.

Link to comment
Share on other sites

You would have to build in a pretty sophisticated algorithm to do this.

 

For example, let's say you have the following two sentences:

 

"I live in a brown house. There is a white picket fence in the front yard"

"I live in a brown house with white trim. There is a white picket fence in the front yard"

 

Well it is pretty easy to "see" the difference, but not as easy to identify it programatically. Right after house there is obviously a change. But, how do you realize "programaticall" that the word white in the 2nd sentence is not the same white from the 1st sentence.

 

In other words, a straitforward approach may show the following differences:

 

"I live in a brown house with white trim. There is a white picket fence in the front yard"

 

when it should be this:

"I live in a brown house with white trim.  There is a white picket fence in the front yard"

 

Link to comment
Share on other sites

If you want to compare sentences like mjdamato's example, below is a rough draft. There's much more to consider, including the valid characters involved and what constitutes a "word."

 

<pre>
<?php
function str_cmp_sentences ($str1, $str2) {
	// Break the strings into sentences.
	$str1_sentences = preg_split('/(\.)\s+/', $str1, -1, PREG_SPLIT_DELIM_CAPTURE);
	$str2_sentences = preg_split('/(\.)\s+/', $str2, -1, PREG_SPLIT_DELIM_CAPTURE);
	// Count.
	$str1_sentences_count = count($str1_sentences);
	$str2_sentences_count = count($str2_sentences);
	// Find largest.
	$number_of_sentences = $str1_sentences_count > $str2_sentences_count ?
		$str1_sentences_count : $str2_sentences_count ;
	// Loop through sentences.
	for ($i = 0; $i <= $number_of_sentences; $i++) {
		// Get "words".
		preg_match_all('/[\w.]+/', $str1_sentences[$i], $str1_words);
		preg_match_all('/[\w.]+/', $str2_sentences[$i], $str2_words);
		$str1_words = $str1_words[0];
		$str2_words = $str2_words[0];
		// Count.
		$str1_word_count = count($str1_words);
		$str2_word_count = count($str2_words);
		// Find largest.
		$number_of_words = $str1_word_count > $str2_word_count ?
			$str1_word_count : $str2_word_count ;
		// Process.
		for ($j = 0; $j <= $number_of_words; $j++) {
			$word1 = $str1_words[$j];
			$word2 = $str2_words[$j];
			$word = $word1 !== $word2 ? 
				'<font color="red">' . $word2 . '</font>' :
				$word2 ;
			$words[] = $word;
		}
		echo join(' ', $words);
		$words = array();
	}
}
$str1 = 'I live in a brown house. There is a white picket fence in the front yard';
$str2 = 'I live in a brown house with white trim. There is a white picket fence in the front yard';
str_cmp_sentences($str1, $str2);
?>
</pre>

Link to comment
Share on other sites

here is code that I have made, but still not working accuratly,

 

<?php

function cek_text($isi1,$isi2)

{

$pj1 = strlen($isi1);

$pj2 = strlen($isi2);

 

if ($pj1 > $pj2)

{

$terpanjang = $pj1;

}

else

{

$terpanjang = $pj2;

}

 

$hasil1 = '';

$hasil2 = '';

for ($i=0;$i<$terpanjang;$i++)

{

if ($isi1[$i] == $isi2[$i])

{

//sama

$hasil1 = $hasil1 . $isi1[$i];

$hasil2 = $hasil2 . $isi2[$i];

}

else

{

//beda

if ($pj1 == $pj2)

{

$hasil1 = $hasil1 .'<font color=red>'. $isi1[$i].'</font>';

$hasil2 = $hasil2 .'<font color=red>'. $isi2[$i].'</font>';

}

elseif ($pj1 > $pj2)

{

$isi2 = substr($isi2,0,$i) . $isi1[$i] . substr($isi2,$i,strlen($isi2));

$hasil1 = $hasil1 .'<font color=red>'. $isi1[$i].'</font>';

$hasil2 = $hasil2 .'<font color=#0000FF>'. $isi2[$i].'</font>';

}

else

{

$isi1 = substr($isi1,0,$i) . $isi2[$i] . substr($isi1,$i,strlen($isi1));

//$hasil1 = $hasil1 .'<font color=red>'. $isi1[$i].'</font>';

$hasil2 = $hasil2 .'<font color=red>'. $isi2[$i].'</font>';

}

 

$pj1 = strlen($isi1);

$pj2 = strlen($isi2);

}

}

 

return($hasil2);

}

 

?>

Link to comment
Share on other sites

And that isn't working? Is it getting some and not others?

 

There are more "foolproof options" for stripping tags.

 

Via php.net:

<?php
function html2txt($document){
$search = array('@<script[^>]*?>.*?</script>@si',  // Strip out javascript
               '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
               '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
               '@<![\s\S]*?--[ \t\n\r]*>@'        // Strip multi-line comments including CDATA
);
$text = preg_replace($search, '', $document);
return $text;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.