I’m trying to compare two string lets say Émilie and Zoey. Well E comes before Z but on the ASCII chart Z comes before É so a normal if ( str1 > str2 ) won’t work.
I tried with if (strcmp(str1,str2) > 0), but that still doesn’t work. So I’m looking into a native way to compare string with UTF-8 characters.
IMPORTANT
This answer is meant for situations where it’s not possible to run/install the ‘intl’ extension, and only sorts strings by replacing accented characters to non-accented characters. To sort accented characters according to a specific locale, using a Collator is a better approach — see the other answer to this question for more information.
Sorting by non-accented characters in PHP 5.2
You may try converting both strings to ASCII using iconv() and the //TRANSLIT option to get rid of accented characters;
Then do the comparison
See the documentation here:
http://www.php.net/manual/en/function.iconv.php
[updated, in response to @Esailija’s remark]
I overlooked the problem of //TRANSLIT translating accented characters in unexpected ways. This problem is mentioned in this question: php iconv translit for removing accents: not working as excepted?
To make the ‘iconv()’ approach work, I’ve added a code sample below that strips all non-word characters from the resulting string using preg_replace().