How do you find the difference between 2 strings in PHP?
-
01-07-2019 - |
Question
I have 2 strings that I'd like to compare, and return the positions of the different characters in the second string.
For example, if I have
"The brown fox jumps over the lazy dog"
"The quick brown fox jumped over the lazy dog"
I want it to highlight "quick" and "ed". What's the best way to go about this in PHP?
Solution
OTHER TIPS
The algorithm you're looking for is the "longest common substring problem". From there it is easy to determine the differences. See Wikipedia:
This is going to give you a headache unless you define your porblem more clearly to start! Let's assume that str1 is "Amanda and Amy", and str2 is "Amanda and Amylase Amy".
Is your function to return "lase Amy" or "Amylase "?
Properly defining your problem is the first step towards a solution!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow