jdepypere
jdepypere

Reputation: 3553

PHP see how many characters are equal

I have a string full of values that are separated by a comma. Needless to say this can be done with $strings = explode(',',$fullstring);.

Now, I need to compare a user-submitted-value to this value to see 'how equal' it is. Ideally I'd like to check that maximum one letter/symbol/number/... is not equal. For example, if the hardcoded string is 'table':

If you understand what I mean. Of course the strings can also contain spaces, apostrophes, numeric values. I really have no idea how I should get started on this. I've already set up a filter to check whether the value is equal (using if(in_array(strtolower($userValue),array_map('strtolower',$strings)))), but in the else clause I need some method to detect what I just explained.

Upvotes: 2

Views: 92

Answers (2)

Guillaume
Guillaume

Reputation: 10961

calculate Levenshtein distance between the two strings: http://php.net/manual/en/function.levenshtein.php and check if the distance is less than some threshold value.

Upvotes: 3

mproffitt
mproffitt

Reputation: 2527

It sounds like the functionality you're looking for is best served by levenshtein.

Have a look at http://php.net/manual/en/function.levenshtein.php for examples and a description of how to use it.

Upvotes: 3

Related Questions