novi Posted February 19, 2007 Share Posted February 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 19, 2007 Share Posted February 19, 2007 Per character or per word? Quote Link to comment Share on other sites More sharing options...
boo_lolly Posted February 19, 2007 Share Posted February 19, 2007 i think he means per character. i'd use some regex with preg_replace() or ereg_replace(). also, strlen() may be of some help. Quote Link to comment Share on other sites More sharing options...
Orio Posted February 19, 2007 Share Posted February 19, 2007 Can you tell us what are the changes made to the string, so it'd be easier to answer your question? (maybe give an example, post some code...) Orio. Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 19, 2007 Share Posted February 19, 2007 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" Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted February 19, 2007 Share Posted February 19, 2007 If he is the one doing the changes to the string, then as the changes are done the highlights can be made...I don't think it's possible to highlight the changes after the fact...at least not accurately. Quote Link to comment Share on other sites More sharing options...
effigy Posted February 19, 2007 Share Posted February 19, 2007 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> Quote Link to comment Share on other sites More sharing options...
novi Posted February 20, 2007 Author Share Posted February 20, 2007 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); } ?> Quote Link to comment Share on other sites More sharing options...
novi Posted February 20, 2007 Author Share Posted February 20, 2007 Thanks all, I think the problem is that there are some HTML TAG inside the sentence that I want to compare. I have tried to clear these TAG using strip_tags() function. But it didn't work. :-\ Quote Link to comment Share on other sites More sharing options...
effigy Posted February 20, 2007 Share Posted February 20, 2007 What code are you using to strip the tags? Is this being done before the strings enter your function? Quote Link to comment Share on other sites More sharing options...
novi Posted February 20, 2007 Author Share Posted February 20, 2007 No, I just added that script to the function, like below: <?php function cek_text($isi1,$isi2) { $isi1 = strip_tags($isi1); $isi2 = strip_tags($isi2); $pj1 = strlen($isi1); $pj2 = strlen($isi2); Quote Link to comment Share on other sites More sharing options...
JBS103 Posted February 20, 2007 Share Posted February 20, 2007 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; } ?> Quote Link to comment Share on other sites More sharing options...
novi Posted February 20, 2007 Author Share Posted February 20, 2007 Thanks for the html2text, JBS103, It is Working now, I just need to make a little changes for it. I will do it now. Thanks. Quote Link to comment 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.