Comparing two unicode strings in PHP

血红的双手。 提交于 2019-12-20 02:01:36

问题


I am stuck in comparing two unicode strings in PHP which both contain the special char 'ö'. One string comes from $_GET, the other one is a filesystem's folder name (scandir()). Both strings seem to be equal to me, making a

var_dump($filter);
var_dump($tail . '/' . $k);

on them also shows their equality but with different string lenghts (?!):

string '/blöb' (length=7)
string '/blöb' (length=6)

My snippet comparing them looks as follows:

if($filter == ($tail . '/' . $k)) {
    /* ... */
}

What's going on here?

Additional information: $tail is an empty string:

string '' (length=0)

回答1:


See here: http://en.wikipedia.org/wiki/Unicode_equivalence and use this: http://www.php.net/manual/en/class.normalizer.php

You probably have a decomposed character in the longer string, meaning an o and then a umlaut combining character which overlays the previous character.

The normalizer function will fix things like that.

As a side note you should always normalize your input if you are using it for equivalence (for example a username - you want to make sure two people don't choose the same username, even if the binary representation of the string happens to be different).




回答2:


Can you try parsing them through utf8_encode() and checking them there? PHP doesn't support unicode and therefore advises to use utf8_encode/decode for some basic Unicode features.

http://php.net/manual/en/language.types.string.php



来源:https://stackoverflow.com/questions/6855425/comparing-two-unicode-strings-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!