mb_ereg_search_init 作为单字节工作?
检查这个片段:(
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
mb_ereg_search_init('καλημέραCCC', 'C+');
$pos = mb_ereg_search_pos();
echo $pos[0];
请不要评论这个特定的示例,这不是我的用例,它是我遇到的问题的减少)
即使字符串“καλημέρα”由 8 个字符组成,上面的片段也会打印 16。 我错过了什么吗? mb_ereg_search_init 不应该支持多字节吗? 如果是的话,是否有任何内置函数可以满足我的需要?
提前致谢。
Check this snippet:
mb_internal_encoding("UTF-8");
mb_regex_encoding("UTF-8");
mb_ereg_search_init('καλημέραCCC', 'C+');
$pos = mb_ereg_search_pos();
echo $pos[0];
(Please don't comment on this specific example, it's not my use case, it's a reduction of the problem I'm having)
Even though the string "καλημέρα" consists of 8 characters, the snippet above prints 16.
Am I missing something? Isn't mb_ereg_search_init supposed to support multi-byte?
And if I am, is there any built-in function that does what I need?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自
mb_ereg_search_pos
< 的手册页/a>:我的解释是它总是返回字节数,而不是实际位置。如果您检查更多这些多字节函数,至少有
如果你想知道第一个
C
的位置,你可以使用mb_strpos
:如果您想不惜一切代价简单地破解它,有一个解决方案。你必须先解码字符串:
字符串变成
?????????CCC
,每个问号正好是1个字节,你可以正确计算它们。但是,如果您现在想在正则表达式中使用多字节字符('λ+'
),它将不起作用。From manual page for
mb_ereg_search_pos
:My interpretation is that it's always returning number of bytes, not the actual position. If you check more of these multi-byte functions, there is at least one more that hints that it's supposed to work this way. Don't ask me what's the purpose of this function then...
If you want to know just position of first
C
, you can usemb_strpos
:If you want to simply hack it at all costs, there's a solution. You have to decode the string first:
String becomes
????????CCC
, each of question marks is exactly 1 byte and you are able to count them properly. However, if you wanna use multi-byte character in regexp now ('λ+'
), it won't work.