PHP 函数 substr() 错误
当我使用 substr()
时,我在最后得到一个奇怪的字符,
$articleText = substr($articleText,0,500);
我输出了 500 个字符,并且 � <--
我该如何解决这个问题?是编码问题吗?我的语言是希腊语。
When I use substr()
I get a strange character at the end
$articleText = substr($articleText,0,500);
I have an output of 500 chars and � <--
How can I fix this? Is it an encoding problem? My language is Greek.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
substr
使用字节而不是字符进行计数。希腊语可能意味着您正在使用某种多字节编码,例如 UTF-8,而按字节计数对于这些编码来说不太好。
也许使用
mb_substr
可能会有所帮助,在这里:mb_*
函数是专门创建的用于多字节编码。substr
is counting using bytes, and not characters.greek probably means you are using some multi-byte encoding, like UTF-8 -- and counting per bytes is not quite good for those.
Maybe using
mb_substr
could help, here : themb_*
functions have been created specifically for multi-byte encodings.使用
mb_substr
代替,它能够处理多种编码,而不仅仅是单字节字符串为substr
:Use
mb_substr
instead, it is able to deal with multiple encodings, not only single-byte strings assubstr
:看起来你正在将一个 unicode 字符切成两半。使用
mb_substr
代替 unicode 安全字符串切片。Looks like you're slicing a unicode character in half there. Use
mb_substr
instead for unicode-safe string slicing.使用这个函数,它对我有用
学分: http://php.net /manual/en/function.mb-substr.php#107698
use this function, It worked for me
Credits: http://php.net/manual/en/function.mb-substr.php#107698
ms_substr() 也可以很好地删除奇怪的尾随换行符,这是我在解析 html 代码后遇到的问题。该问题不是通过以下方式处理的:
或:
或:
不要抓住。
ms_substr() also works excellently for removing strange trailing line breaks as well, which I was having trouble with after parsing html code. The problem was NOT handled by:
or:
or:
Don't catch.
UTF-8 编码字符串的替代解决方案 - 这将在切割子字符串之前将 UTF-8 转换为字符。
要将articleText字符串恢复为UTF-8,需要额外的操作:
Alternative solution for UTF-8 encoded strings - this will convert UTF-8 to characters before cutting the sub-string.
To get the articleText string back to UTF-8, an extra operation will be needed:
你正在尝试剪切 unicode 字符。所以我更喜欢在 php 中尝试
mb_substr()
而不是substr()
。substr()
mb_substr()
有关 substr() 的更多信息 - 制作人员 =>检查这里
You are trying to cut unicode character.So i preferred instead of
substr()
trymb_substr()
in php.substr()
mb_substr()
For more information for substr() - Credits => Check Here