PHP compare string with accents and without accent

2019-06-12 04:03发布

问题:

Is it possible in PHP to compare thoses strings:

Æther == AEther == Aether

I'd like to get a positive result from this equivalence

I've actually tried multiple things but without real success:

  • Replacing the Æ and any special character to Ae with strtr (bad performance and I would rather keep the string as is)

  • Using strcmp/strcasecmp, this solve the caps problem but I still have trouble with all UTF-8 characters

What I'm trying to achieve it's to parse a list of elements retrieved from a json and match with some other json file, some can be spelled differently (utf8 or non utf8, caps etc.) and so, for now, the only way I found to do this it's to make a third json like this:

        {
        "match": {
            "name": "Unravel the \u00c6ther"
        },
        "replace": {
            "name": "Unravel the aether"
        }

And I replace the base string with the corect one, but I'd like to find a way to automatise the process.

回答1:

You can use iconv's transliterate feature:

iconv('utf8', 'ASCII//TRANSLIT', 'Æther') == 'Aether';

Some Windows systems may required the use of 'utf-8' instead of 'utf8'.



回答2:

You need to write a function doing that. I give you two hints:

  • strtolower
  • levenshtein

This two function can get you started ;)



回答3:

Why not use str_replace() to replace Æ with AE. Then use 'strtolower()' to convert both strings to lower case and compare...



标签: php string utf-8