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
https://forums.phpfreaks.com/topic/39128-see-difference-between-two-string/
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"

 

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>

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);

}

 

?>

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;
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.