西里尔字符串的Php长度使其值增加一倍 [英] Php length of cyrillic string doubles its value

查看:145
本文介绍了西里尔字符串的Php长度使其值增加一倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好这里是问题:
当我得到$ _POST拉丁字符串strilen()工作完美,但当我得到西里尔字符串strlen()加倍它的值在这里是代码:

Hello here it is the problem: when i get to the $_POST latin string strilen() works perfectly, but when i get cyrillic string strlen() doubles its value here is the code:

$word = $_POST['word'];
echo strlen($word) . '<br>'; //input: abc -> returns 3, input: абв -> returns 6 
var_dump($word); //input: abc -> returns string 'abc' (length=3), input: абв -> returns string 'абв' (length=6)

你有一些想法吗?

Do you have some ideas?!

推荐答案

strlen 不会重复任何事情,它只是报告情况。具体来说,它会报告多少个字符,而不是多少个字符组成字符串。那是因为 strlen 不知道什么是字符,盲目地假定1个字节= 1个字符。因此,我们说 strlen 不是多字节感知。

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".

在你的情况下,看起来浏览器将UTF-8编码数据提交到服务器。在UTF-8中,西里尔字符是每个字符两个字节。

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.

如果要查找字符串中的字符数,请使用多字节感知 mb_strlen

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');

这篇关于西里尔字符串的Php长度使其值增加一倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆