PHP mb_substr() not working correctly?

允我心安 提交于 2020-01-01 07:33:07

问题


This code

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

But it's not right, because (0, 2) means 2 characters...


回答1:


Try passing the encoding parameter to mb_substr, as such:

print mb_substr('éxxx', 0, 1, 'utf-8');

The encoding is never detected automatically.




回答2:


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.

Setting a good default will probably fix this and some other issues:

mb_internal_encoding('UTF-8');


来源:https://stackoverflow.com/questions/13953248/php-mb-substr-not-working-correctly

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