2

I want to use string functions and do not depend on severs configuration in terms of overloading and default char-set.

Say, when I call strtoupper($param) it should be mb_strtoupper($param, 'UTF-8') in many cases.

This is definitely the framework responsibility. But I can't find the right wrapper in M2.

There are some functions in \Magento\Framework\Stdlib\StringUtils $string but not all.

M2 core code sometimes calls mb_
\vendor\magento\module-dhl\Model\Carrier.php
\vendor\magento\module-catalog-import-export\Model\Import\Product.php
sometimes iconv_
\vendor\magento\module-paypal\Model\Report\Settlement.php

What is the right and recommended approach?

4
  • welcome back Amasty. why you have my edit :). you do not like this edit?
    – Amit Bera
    Jul 31 '16 at 7:08
  • is this sting functions or string function?
    – Amit Bera
    Jul 31 '16 at 7:36
  • Hi Amit, thank you for the note, fixed it :) Do you know something about strings logic in M2?
    – Amasty
    Jul 31 '16 at 8:25
  • i do have good idea on topics
    – Amit Bera
    Jul 31 '16 at 9:16
2

Thats definitely an interesting question.

So here's the thing: the mb_string library is a requirement of Magento 2 (and as a side note the iconv library is also a requirement if you run PHP7)

So you won't be able to install M2 without it.

Thus Magento code don't care much about checking if mb_ methods exist or not.

It seems like most of the time direct calls to the PHP methods are done without using a Magento string class for example:

The only exception I found is this one I'm not too sure if it's relevant though: https://github.com/magento/magento2/blob/6ea7d2d85cded3fa0fbcf4e7aa0dcd4edbf568a6/app/code/Magento/GoogleAdwords/Model/Filter/UppercaseTitle.php#L20

So I reckon you should be able to use those methods directly like Magento does.

Regarding the iconv methods I find it disturbing indeed. Some code checks if the library is implemented some other code simply use the @ operator to mute the errors (which is not great right ?) so I'm not sure about those.

3
  • Thank you for the sharing the ideas. What is the best place, to your opinion, to set default encoding? In magento 1 I see numerous cases in support request when we had to use mb_strtoupper($param, 'UTF-8') instead of just mb_strtoupper($param). Or should we use mb_internal_encoding on app launch?
    – Amasty
    Aug 1 '16 at 14:21
  • @Amasty honestly I'm surprised m2 does not handle that properly out of the box I feel like this is a bug. Did you try submitting an issue on github ? Aug 1 '16 at 15:45
  • May be we need to submit it, but I prefer to have some ready to use solution, as our clients can not wait )
    – Amasty
    Aug 1 '16 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.