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.
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.