After searching both Php Builder and Google for quite some time I give up my searching and ask the question here instead.

I have a pretty simple article management system, where my users can edit my articles. But before the edited text is inserted into the database one of my admins have to accept the new / edited text.

However, if a users edit consist of just a corrected spelling error in the middle of the string it is quite hard for the admin to see what has been changed.

So my question is: Is there an 'easy' way to compare two strings (TEXT field in mysql) and somehow mark the differences between the two strings?

(Note: The corrected spelling error was just an example, usually a sentence has just been added.)

Thanks in advance / jek

    Well, it depends.

    You would have to store both versions while waiting for the admin to read the new version?

    As for the comparison:
    You could use similar_text() to check the string, then do a preg_match_all() on string_old in string_new.

    Or perhaps soundex to check if it has changed:
    http://no.php.net/manual/en/function.soundex.php

    But none of these do it really good.
    Depending on the size of the text: perhaps do an explode() on the spaces on both versions, then compare word for word the two arrays?

    knutm :-)

      Thanks for your reply, but the problem is that I don't want to just check if the text has changed, I want to mark the changes.

      Example:

      Old string: There onse was a man in nantucket

      New string: There once was a man from Nantucket

      So, now that I have both strings, I want to output the new string with the changes highlighted in some way:

      Result: There once was a man from Nantucket

      It doesn't have to be exactly like this, I just want to make it easier for my admins to check the changes in the text (since the articles can be quite long)...

      Any ideas?

        3 years later

        No, I actually never did.

          thanks, the closest I've come so far is using DIFF command as a system/unix command....

          thought this would be easier...

            First of all, it sounds like by "string comparison" you really mean "word-by-word comparison." In otherwords, a literal character-by-character match wouldn't do you any good:

            Old string: There onse was a man in nantucket
            New string: There once was a man from Nantucket

            Lining the two strings up like that shows you what I mean... "from Nantucket" would be considered "different" if you did a character-by-character analysis.

            Instead, mogster's suggestion of exploding on spaces might work. You could then take each word of both strings and do a character-by-character analysis of those words. Something like this:

            function compareStrings($oldString, $newString) {
            	$old_array = explode(' ', $oldString);
            	$new_array = explode(' ', $newString);
            
            for($i=0; isset($old_array[$i]) || isset($new_array[$i]); $i++) {
            	if(!isset($old_array[$i])) {
            		echo '<font color="red">' . $new_array[$i] . '</font>';
            		continue;
            	}
            
            	for($char=0; isset($old_array[$i]{$char}) || isset($new_array[$i]{$char}); $char++) {
            
            	if(!isset($old_array[$i]{$char})) {
            		echo '<font color="red">' . substr($new_array[$i], $char) . '</font>';
            		break;
            	} elseif(!isset($new_array[$i]{$char})) {
            		break;
            	}
            
            	if(ord($old_array[$i]{$char}) != ord($new_array[$i]{$char}))
            		echo '<font color="red">' . $new_array[$i]{$char} . '</font>';
            	else
            		echo $new_array[$i]{$char};
            
            	}
            
            	if(isset($new_array[$i+1]))
            		echo ' ';
            
            }
            }

            Somethings to know about this function I created:

            • If the word in the "new" string is shorter than the corresponding word in the old string, no extra highlighting will be done. You can change the behavior of this, but I just didn't know how you wanted that situation handled.
            • This function will print out the newly highlighted string. If you instead wanted to store it's contents in a variable, some modifications would be needed.
            • I'm a bit tired this morning, so there are probably some optimizations you could do (especially if you're using really large strings). But for the most part, I think it's okay...
              Write a Reply...