substr不能与utf8一起正常工作 [英] substr doesn't work fine with utf8
问题描述
我正在使用substr方法访问字符串的前20个字符.在正常情况下,它可以正常工作,但是在rtl语言(utf8)上运行时,它给我错误的结果(显示了大约10个字符).我已经在网上搜索过,但发现第n个对于解决此问题有用的方法.这是我的代码行:
I am using a substr method to access the first 20 characters of a string. It works fine in normal situation, but while working on rtl languages (utf8) it gives me wrong results (about 10 characters are shown). I have searched the web but found nth useful to solve this issue. This is my line of code:
substr($article['CBody'],0,20);
谢谢.
推荐答案
如果您使用编码为UTF-8的字符串,则可能会丢失 当您尝试使用PHP substr获取字符时,请使用其中的字符 功能.发生这种情况是因为UTF-8中的字符不受限制 到一个字节,它们具有可变长度以匹配Unicode字符, 在1到4个字节之间.
If you’re working with strings encoded as UTF-8 you may lose characters when you try to get a part of them using the PHP substr function. This happens because in UTF-8 characters are not restricted to one byte, they have variable length to match Unicode characters, between 1 and 4 bytes.
您可以使用 mb_substr() ,它几乎可以正常工作与substr相同,但不同之处在于您可以添加一个新参数来指定编码类型,无论是UTF-8
还是其他编码.
You can use mb_substr(), It works almost the same way as substr but the difference is that you can add a new parameter to specify the encoding type, whether is UTF-8
or a different encoding.
尝试一下:
$str = mb_substr($article['CBody'], 0, 20, 'UTF-8');
echo utf8_decode($str);
希望这会有所帮助.
这篇关于substr不能与utf8一起正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!