When to use mb_strpos (); over strpos () ;?

Yes, looking at all these string functions, sometimes they confuse me. One uses all the mb_ time mb_ , the others simple, so the question is simple ...

When should I use mb_strpos(); and when should I go with simple ( strpos(); )?

And yes, I know that mb_ functions are multibyte, but does this really mean that if I only work with utf-8 encoded strings, should I stick with the mb_ function?

Thanks in advance!

+2
source share
3 answers

You should use the mb_ functions whenever you expect to work with text that is not pure ASCII. That is, you can work with regular string functions, even if you use UTF-8, if all the strings you use for them contain only ASCII characters.

 strpos('foobar', 'foo') // fine in any (ASCII-compatible) encoding, including UTF-8 strpos('ふーばー', 'ふー') // won't work as expected, use mb_strpos instead 
+6
source

Yes, if you work with UTF-8 (this is a multibyte encoding: one character can use more than one byte), you should use the mb_* functions.

Functions other than mb will work with bytes rather than characters - this is normal if 1 character == 1 byte; but it is not (for example) UTF-8.

+4
source

I would say yes, here is a description from php documentation:

mbstring provides multibyte-specific string functions that help handle multibyte encodings in PHP. In addition to this, mbstring handles character encoding conversion between possible encoding pairs. mbstring is designed to handle Unicode-based encodings such as UTF-8 and UCS-2, and many single-byte encodings for convenience ....

If you are not sure if the mb extension is loaded, you should check this before because the mb line is not a default extension.

+3
source

All Articles