Search code examples
phputf-8strpos

mb_strpos vs strpos, what's the difference?


Yes: I know. We should use mb_* function when we're working with multibyte char. But when we're using strpos? Let's take a look this code (saved in utf-8)

var_dump(strpos("My symbol utf-8 is the €.", "\xE2\x82\xAC")); // int(23)

There is a difference of using mb_strpos? Does't makes this work the same jobs? After all, does't strpos seek a string (multiple byte)? Is there a reason to use instead strpos?


Solution

  • For UTF-8, matching the byte sequence is exactly the same as matching character sequence.

    So they both will find the needle at exactly the same point, but mb_strpos counts full UTF-8 byte sequencees before the needle, where as strpos calculates any bytes. So if your string had another multi-byte UTF-8 sequence, the results would be different:

    strpos("My symbolö utf-8 is the €.", "€") !== mb_strpos("My symbolö utf-8 is the €.", "€", 0, "UTF-8")
    

    But:

    strpos("My symbol utf-8 is the €.", "€") === mb_strpos("My symbol utf-8 is the €.", "€", 0, "UTF-8")