我应该重构所有框架以使用 mbstring 函数吗?

发布于 2024-12-21 17:45:01 字数 143 浏览 6 评论 0原文

我目前使用 mbstring.func_overload = 7 来处理 UTF-8 字符集。

我正在考虑重构所有 func 调用以使用 mb_* 函数。

您认为这是必然的吗?或者在 PHP 6 或更高版本中,多字节问题将以另一种方式解决?

I currently use mbstring.func_overload = 7 to get working with UTF-8 charset.

I am thinking to refactor all func call to use mb_* functions.

Do you think this is necessarily, or with PHP 6 or newer version the multibyte problem will be solved in another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

司马昭之心 2024-12-28 17:45:02

如果您正在使用其他人创建的库,则不建议使用。以下是三个原因。

  1. 重载可能会破坏不需要重载的库的行为。
  2. 您的框架可以在不超载的环境中被破坏。
  3. 依赖重载会减少框架的潜在用户,因为 2

1. 的一个很好的例子是使用 strlen 错误计算了 HTTP Content-Length 字段中的字节大小。原因是重载的strlen函数不返回字节数,而是返回字符数。您可以在 CakePHPZend_Http_Client

编辑:
PHP 5.5 或 5.6 正在考虑弃用 mbstring.func_overload (来自 mbstring 维护者的邮件 2012 年 4 月)。所以现在您应该避免mbstring.func_overload。

对于各种平台处理多字节字符的推荐策略是直接使用 mbstring 或 intl 或 iconv。如果您确实需要后备函数来处理多字节字符,请使用function_exists()

您可以在WordpressMediaWiki中查看案例。

  1. WordPress: wp-includes/compact.php
  2. MediaWiki : 后备类

一些 CMS 喜欢Drupal (unicocde.inc) 引入多字节抽象层。

我认为抽象层不是一个好主意。
原因是在很多情况下所需的处理多字节函数的数量低于 10 个,并且 umultibyte 函数易于使用,但如果安装了这些模块,则会降低将处理切换到 mbstring 或 intl 或 iconv 时的性能。

处理多字节字符的最低要求是mb_substr()并处理无效的字节序列。
您可以在上述 CMS 中看到 mb_substr() 的回退函数的情况。
我在以下位置回答了有关处理无效字节序列的问题: 用问号替换无效的 UTF-8 字符,mbstring.substitute_character 似乎

Not recommended if you are using the libraries other people create. Here are three reasons.

  1. Overloading can break the behaviors of the libraries that don't expect overloading.
  2. Your framework can be broken in the environments without overloading.
  3. Depending on overloading decreases the prospective users of your framework because of 2

Good example of 1. is miscaliculation of bytesize in HTTP Content-Length field by using strlen. The cause is that the overloaded strlen function does not return the number of bytes but number of characters. You can see real world issues in CakePHP and Zend_Http_Client.

Edit:
deprecating mbstring.func_overload is under consideration in PHP 5.5 or 5.6 (from mbstring maintainer's mail in 2012 April). So now you should avoid mbstring.func_overload.

The recommended policy of handling mutibyte characters for various platforms is to use mbstring or intl or iconv directlly. If you really need fallback functions for handling multibyte characters, use function_exists().

You can see the cases in Wordpress and MediaWiki.

  1. WordPress: wp-includes/compact.php
  2. MediaWiki: Fallback Class

Some of CMSes like Drupal (unicocde.inc) introduce mutibyte abstraction layer.

I think the abstraction layer is not good idea.
The reason is that the number of handling multibyte functions needed in a lot of case is under 10 and umultibyte functions are easy to use and decrease perfomance for switching the handling to mbstring or intl or iconv if these module are installed.

The minimum requirement for handling multibyte characters is mb_substr() and handling invalid byte sequence.
You can see the cases of a fallback function for mb_substr() in the above CMSes.
I answered about handling invalid byte sequence in the following place: Replacing invalid UTF-8 characters by question marks, mbstring.substitute_character seems

暗喜 2024-12-28 17:45:02

对于 utf-8 的字符串(当然)

的,当然。不过,您可以使用字符串做很多事情。 UTF-8 向后兼容 ASCII。如果您只想对字符串的 ASCII 字符进行操作,则可能会产生影响,也可能不会产生影响。这取决于您需要如何处理您的字符串。

如果您想要一个直接的答案:,您不应该将每个函数重构为 mb_ 函数,因为这可能有点矫枉过正。您是否应该检查您的用例,多字节 UTF-8 字符串是否会影响结果并进行相应的重构?是的。

for string that are utf-8 (of course)

Yes, of course. There are many things you can do with strings though. UTF-8 is backwards compatible with ASCII. If you only want to operate on the ASCII characters of a string, it may or may not make a difference. It depends on what you need to do with your strings.

If you want a direct answer: No, you should not refactor every function to an mb_ function, because it's likely overkill. Should you check your use cases whether a multi-byte UTF-8 string may impact results and refactor accordingly? Yes.

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