Reputation: 131
This is my code:
$result = "";
$str = "Тугайный соловей";
for ($y=0; $y < strlen($str); $y++){
$tmp = substr($str, $y, 1);
$result = $result.$tmp;
}
echo "result = ".$result;
$result gives: Тугайный Ñоловей
I'm looking for two weeks to solve this problem reading all kinds of articles but still .....
Thank you very much!
Upvotes: 1
Views: 846
Reputation:
Just to elaborate on Juanmi's answer re: charset types (and since I already took the screenshots)...
Your code appears to work when the right encoding is specified, but it's treating each byte as its own character, which is not the correct way to handle UTF-8. You probably need the multibyte-aware string functions.
Upvotes: 1
Reputation: 597
I've tried your code and I see the right string in UTF-8 codification. If the page charset, or browser is set to ISO-8859-1 I get the wrong string. So using the right codification should be enough.
Upvotes: 1
Reputation: 5243
You are using functions that are not made for UTF-8 strings on a UTF-8 string
Use mb_substr instead
edit: the answer above by xeoncross is the same, use his code :)
Upvotes: 0
Reputation: 57294
You need to use the multi-byte functions not the plain string functions.
$result = "";
$str = "Тугайный соловей";
for ($y=0; $y < mb_strlen($str); $y++){
$tmp = mb_substr($str, $y, 1);
$result = $result.$tmp;
}
echo "result = ".$result;
Upvotes: 4