简体   繁体   English

错误strlen函数未正确计算php中的单词

[英]ERROR strlen function is not count properly of words in php

Please help me. 请帮我。
$string = "Subdivision"; $ string =“细分”; echo strlen($string); echo strlen($ string);

Output: 23;

I want to output Output: 11; 我想输出Output:11;

I believe your string is encoded as UTF-16 and strlen is calculating it as UTF-8 我相信您的字符串编码为UTF-16,strlen将其计算为UTF-8

Either your php file's character encoding is in UTF-16 or the string comes from source that encodes it in UTF-16 您的php文件的字符编码采用UTF-16格式,或者该字符串来自使用UTF-16对其进行编码的源

使用mb_strlen函数:

echo mb_strlen($string);

you can convert your variable encoding first before strlen 您可以在转换前先转换变量编码

$string = "Subdivision";
// $string = mb_convert_encoding($string, "UTF-16"); //just a test and its print 22 as you and user3647971 said
$string = mb_convert_encoding($string, "UTF-8");
echo strlen($string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM