开发者

Finding bytes in strings with PHP's mbstring.func_overload on

开发者_开发问答I have PHP configured with mbstring.func_overload = 7, so all the single-byte-string functions are mapped to their multi-byte equivalents. But I still sometimes need to treat strings as byte arrays; for example, when calculating their size or doing encryption.

What's the best approach here? Can I just use the multi-byte functions and pass them a single-byte encoding, even though that's not actually how the string is encoded? For example:

mb_substr($utf8str, 0, 1, "latin1");
mb_strlen($utf8str, "latin1");

EDIT: I noticed when looking through PHP's source that they rename the original functions to mb_orig_X, as in mb_orig_strlen. Probably not safe to use, as they're not documented, but interesting.


I think you shouldn't be overriding these functions if you need to use the original ones (i.e., if you really need to operate on binary strings), it is quite a dirty solution. This forces you to make an even dirtier workaround for that choice you made earlier. And it possibly breaks libraries you are using without you being aware of that (but the PHP team keeps inventing more and more stupid features like that).

But if you must keep it that way, you should:

  1. use a language-neutral encoding like ASCII (not for the interpreter, but for those reading your code - even if that's you in 2 years.) and
  2. document why you did that thoroughly, since it will be very confusing for everyone looking into that piece of code.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜