0

Is it possible to return the number of characters which differ between two strings?

In my case, I would like to run a check to quantify how much a user's draft has changed vs the database version. With this information, the system can decide weather or not the draft needs to be auto-saved.

For example:

echo string_diff("Hello world", "Hi World");

This should output "5", indicating that characters "ello" & "W" are different.

Is this possible with PHP? How about with mySQL?

cronoklee
  • 6,482
  • 9
  • 52
  • 80

1 Answers1

3

On the PHP side your options are

  1. similar_text which calculates the percent similarity between two strings
  2. levenshtein - which calculates the edit distance between two strings (the number of key strokes it takes to transform string a into string b)

on mysql you can use this user defined function to calculate the levenshtein distance

Orangepill
  • 24,500
  • 3
  • 42
  • 63