mb_strlen 是否适合替代 iconv_strlen

发布于 2024-08-18 01:26:12 字数 121 浏览 5 评论 0原文

也就是说,如果我完全用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高冷爸爸 2024-08-25 01:26:12

如果您想要直接替换普通 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文