PHP mb_substr() 无法正常工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13953248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP mb_substr() not working correctly?
提问by Alex
This code
这段代码
print mb_substr('éxxx', 0, 1);
print mb_substr('éxxx', 0, 1);
prints an empty space :(
打印一个空白空间:(
It is supposed to print the first character, é. This seems to work however:
它应该打印第一个字符,é. 然而,这似乎有效:
print mb_substr('éxxx', 0, 2);
print mb_substr('éxxx', 0, 2);
But it's not right, because (0, 2) means 2 characters...
但这是不对的,因为 (0, 2) 表示 2 个字符......
回答by povilasp
Try passing the encoding parameter to mb_substr, as such:
尝试将编码参数传递给 mb_substr,如下所示:
print mb_substr('éxxx', 0, 1, 'utf-8');
The encoding is never detected automatically.
编码永远不会自动检测。
回答by álvaro González
In practice I've found that, in some systems, multi-byte functions default to ISO-8859-1 for internal encoding. That effectively ruins their ability to handle multi-byte text.
在实践中,我发现在某些系统中,多字节函数默认使用 ISO-8859-1 进行内部编码。这实际上破坏了他们处理多字节文本的能力。
Setting a good default will probably fix this and some other issues:
设置一个好的默认值可能会解决这个问题和其他一些问题:
mb_internal_encoding('UTF-8');

