Posted By

jatkins on 06/13/11


Tagged

diff text difference comparison


Versions (?)

Basic text diff


 / Published in: PHP
 

Released into the public domain. Can be a little buggy, can break up HTML (you may wish to remove HTML before running text through this function).

  1. function label_words($text) {
  2. $times_labeled = array();
  3. $words = explode(' ', $text);
  4. $new_words = array();
  5. for($i=0;$i<count($words);$i++) {
  6. if(isset($times_labeled[$words[$i]]))
  7. $times_labeled[$words[$i]]++;// = $times_labeled[$words[$i]]++;
  8. else
  9. $times_labeled[$words[$i]] = 0;
  10. $new_words[$times_labeled[$words[$i]] . '_' . $words[$i]] = '';
  11. }
  12. return $new_words;
  13. }
  14.  
  15. function diff($text1, $text2) {
  16. $text1_words = label_words($text1);
  17. $text2_words = label_words($text2);
  18. $added_words = array_diff_key($text2_words, $text1_words);
  19. $removed_words = array_diff_key($text1_words, $text2_words);
  20. $text1_output = '';
  21. foreach($text1_words as $text1_word => $empty) {
  22. $original_word = substr($text1_word, strpos($text1_word, '_') + 1);
  23. $text1_output .= isset($removed_words[$text1_word]) ? "<del>$original_word</del> " : "$original_word ";
  24. }
  25. $text2_output = '';
  26. foreach($text2_words as $text2_word => $empty) {
  27. $original_word = substr($text2_word, strpos($text2_word, '_') + 1);
  28. $text2_output .= isset($added_words[$text2_word]) ? "<ins>$original_word</ins> " : "$original_word ";
  29. }
  30. return array($text1_output, $text2_output);
  31. }

Report this snippet  

You need to login to post a comment.