我什么时候应该使用 mb_strpos();通过 strpos();?
呵呵,看着所有这些字符串函数,有时我会感到困惑。一个是一直使用 mb_
函数,另一个是普通函数,所以问题很简单......
我什么时候应该使用 mb_strpos();
以及我应该什么时候去与普通的(strpos();
)?
而且,是的,我知道 mb_
函数代表多字节,但这真的意味着,如果我只使用 utf-8 编码的字符串,我应该坚持使用 <代码>mb_功能?
提前致谢!
Huh, looking at all those string functions, sometimes I get confused. One is using all the time mb_
functions, the other - plain ones, so the question is simple...
When should I use mb_strpos();
and when should I go with the plain one (strpos();
)?
And, yes, I'm aware about that mb_
functions stand for multi-byte, but does it really mean, that if I'm working with only utf-8 encoded strings, I should stick with mb_
functions?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您希望处理非纯 ASCII 文本时,您应该使用
mb_
函数。即,即使您使用的是 UTF-8,只要您使用它们的所有字符串仅包含 ASCII 字符,您就可以使用常规字符串函数。You should use the
mb_
functions whenever you expect to work with text that's not pure ASCII. I.e. you can work with the regular string functions, even if you're using UTF-8, as long as all the strings you're using them on only contain ASCII characters.是的,如果使用 UTF-8 (这是一种多字节编码:一个字符可以使用多个字节),您应该使用
mb_*
函数。非 mb 函数适用于字节,而不是字符——当 1 个字符 == 1 个字节时,这很好;但(例如) UTF-8 的情况并非如此。
Yes, if working with UTF-8 (which is a multi-byte encoding : one character can use more than one byte), you should use the
mb_*
functions.The non-mb functions will work on bytes, and not characters -- which is fine when 1 character == 1 byte ; but that's not the case with (for example) UTF-8.
我想说是的,这是 php 文档中的描述:
mbstring 提供了多字节特定的字符串函数,可以帮助您处理 PHP 中的多字节编码。除此之外,mbstring 还处理可能的编码对之间的字符编码转换。 mbstring 旨在处理基于 Unicode 的编码,例如 UTF-8 和 UCS-2 以及许多单字节编码,以方便使用....
如果您不确定是否加载了 mb 扩展,您应该先检查一下,因为 mb-string 是非默认扩展。
I'd say yes, here's the description from the php documentation:
mbstring provides multibyte specific string functions that help you deal with multibyte encodings in PHP. In addition to that, mbstring handles character encoding conversion between the possible encoding pairs. mbstring is designed to handle Unicode-based encodings such as UTF-8 and UCS-2 and many single-byte encodings for convenience....
If you're not sure that the mb extension is loaded, you should check before because mb-string is a non-default extension.