mb_strlen 是否适合替代 iconv_strlen
也就是说,如果我完全用 PHP4 编写一些东西?或者也许我应该使用自定义函数或类/扩展来计算多字节字符串中的字符数?
我能发现的唯一区别是 mb_string 去掉了坏序列,而 iconv_strlen 则没有。
That is, if I'm coding something entirely in PHP4? Or perhaps I should use a custom function or class/extension instead to count the number of characters in a multibyte string?
Only difference I can spot is that mb_string strips out bad sequences, while iconv_strlen doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想要直接替换普通 strlen,请使用 mb_strlen,因为它始终返回 int。虽然这是非常有争议的(iconv 的正确性超过了 mb 的容错性),但实际上 mb_strlen 的容错能力对我来说更好。只需确保在 php.ini 中或应用程序的中心位置将 mb 配置为所需的编码即可。
PHP 中的 Unicode 支持很糟糕,您必须意识到许多陷阱和异常。在将几个大型应用程序及其用户数据完全切换为 UTF-8 后,我简直要哭了。
If you want a drop-in replacement for plain strlen, use mb_strlen as it always returns an int. This is very debatable though (iconv's correctness over mb's tolerance), but in practice mb_strlen's fault tolerance served me better. Just make sure you configure mb to the desired encoding either in php.ini or in a central place in your application.
Unicode support in PHP is in a bad place, you have to be aware of many pitfalls and exceptions. Having done a complete switch of several large applications and their user data to UTF-8, I could cry you a river.