1

I have a string in UTF-8.

$string = "ãçé êíõ";

I need to find the position of the space. I have to use mb_strpos($string,' '); ?

2 Answers2

3

If you want to find the character position of the space, then yes. strpos will not do because the byte value 0x20 (UTF-8 code point for space) can also be encountered as part of a code point encoded over multiple bytes.

Also, do not forget to specify the encoding -- either explicitly on the mb_strpos call or by setting a default with mb_internal_encoding.

Jon
  • 428,835
  • 81
  • 738
  • 806
2

Note that just using mb_strpos may not be sufficient - there are several code points that display as a space. See e.g. this list - the second most common space character (second to the default ASCII space U+0020, decimal 32) is the nonbreakable space (U+00A0); you may need to check for that one, too - or replace it into the "usual" space character.

Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222