mb_strpos vs strpos, what's the difference?

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")

Depending on the character set in use and the string being searched for, this may or may not make a difference.

strpos() looks for the byte sequence that is passed as the needle.

mb_strpos() does the same thing but it also respects character boundaries.

So strpos() will match if the byte sequence occurs anywhere in the string. mb_strpos() will only match if the byte sequence also represents a valid set of complete characters.

Tags:

Php

Utf 8

Strpos