Theo Smeets
Theo Smeets

Reputation: 131

"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!

Upvotes: 1

Views: 846

Answers (4)

user7675
user7675

Reputation:

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

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

UTF-8

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

ISO-8859--1

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

Juanmi Rodriguez
Juanmi Rodriguez

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

Sqoo
Sqoo

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

Xeoncross
Xeoncross

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

Related Questions