在 UTF-8 字符串中查找空格,是否需要 mb_strpos?
我有一个 UTF-8 格式的字符串。
$string = "ãçé êíõ";
我需要找到空间的位置。 我必须使用 mb_strpos($string,' '); ?
I have a string in UTF-8.
$string = "ãçé êíõ";
I need to find the position of the space.
I have to use mb_strpos($string,' ');
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想找到空格的字符位置,那么可以。
strpos
不会这样做,因为字节值0x20
(空格的 UTF-8 代码点)也可能作为代码点 通过多个字节进行编码。另外,不要忘记指定编码 - 要么在 mb_strpos 调用中显式指定,要么通过使用
mb_internal_encoding
。If you want to find the character position of the space, then yes.
strpos
will not do because the byte value0x20
(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 withmb_internal_encoding
.请注意,仅使用 mb_strpos 可能还不够 - 有多个代码点显示为空格。请参见例如 此列表 - 第二个最常见的空格字符(第二个)默认 ASCII 空格
U+0020
,十进制32
)是不可破坏的空格 (U+00A0
);您可能还需要检查该字符 - 或者将其替换为“常用”空格字符。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 spaceU+0020
, decimal32
) is the nonbreakable space (U+00A0
); you may need to check for that one, too - or replace it into the "usual" space character.