开发者

PHP :: handle typos in search

开发者 https://www.devze.com 2023-01-22 16:15 出处:网络
I have a field to input your friend\'s name and search him up to add him as friend. The problem is that if you are not sure how to spell his name like:

I have a field to input your friend's name and search him up to add him as friend.

The problem is that if you are not sure how to spell his name like:

Kris ins开发者_如何学Gotead of Chris for example.

I think the right way to do this is use RegEx but I don't know how...

  • How can I handle those kind of typos in PHP ?

    (if it isn't possible in PHP then tell me how to do it in jQuery)


I think a regex is not a good approach to check for typos and similar strings. I would consider something like levenshtein - PHP even has a native function for that, levenshtein.

EDIT: Depending on what you're looking for, there are other algorithms too that are also native in PHP: soundex (although considered superseded by newer approaches like Double Metaphone), metaphone, similar_text.


Regex isn't appropriate for this. Your best bet is to create a set of names that group similar names with their spellings. So the name Chris would search for Kris, Chris, Kriss, etc.

A hash_map is probably a good choice. Levenshtein distance could sorta work, but you'll get a lot of false positives: (Camella vs Pamella for example).

0

精彩评论

暂无评论...
验证码 换一张
取 消