Wikis & learning

Compare two short PHP strings

Use edit distance when the question is how many character edits separate two short strings. A similarity percentage answers a different question and can yield a different ranking.

Question

Compare two short PHP strings

Editorial answer

Use edit distance when the question is how many character edits separate two short strings. A similarity percentage answers a different question and can yield a different ranking.

Call levenshtein on normalized strings and sort by ascending distance. Choose normalization consciously: lowercasing may help for identifiers, while punctuation may matter for names. PHP string processing can be byte-oriented, so evaluate multibyte text with examples from your own language.

Verify the result

The distance between cat and cart is one insertion. A distance of zero should only occur for matching normalized strings with standard costs.

Discussion

Sign in to participate.