Php length of cyrillic string doubles its value

独自空忆成欢 提交于 2019-12-02 02:35:20

strlen does not double anything, it simply reports what the situation is. Specifically, it reports how many bytes -- and not how many characters -- make up the string. That is because strlen does not have any knowledge of what a "character" is, and blindly assumes that 1 byte = 1 character. Therefore we say that "strlen is not multibyte-aware".

In your case, it seems that the browser submits UTF-8 encoded data to the server. In UTF-8, cyrillic is two bytes per character.

If you want to find out the number of characters in the string, use the multibyte-aware mb_strlen:

echo mb_strlen($word, 'UTF-8');

Try mb_strlen() if you're dealing with multi-byte characters.

http://php.net/manual/en/function.mb-strlen.php

if you want to get length in the terms of characters (not bytes) use a multi-byte version of strlen: mb_strlen: http://php.net/manual/en/function.mb-strlen.php

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