开发者

"strange" characters

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!


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;


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.


Just to elaborate on Juanmi's answer re: charset types (and since I already took the screenshots)...

Content-type: text/html; charset=utf-8

"strange" characters

Content-type: text/html; charset=iso-8859-1

"strange" characters

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.


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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜