anon
anon

Reputation:

Accessing chars in multibyte string php

I have mbstring.func_overload = 7 and using UTF-8. Everything works fine but this not:

$str = "ãçéíõ";
echo $str[0];

It prints a question mark in the browser.

This instead works normally:

echo substr($str,0,1);

Someone knows why?

Upvotes: 0

Views: 85

Answers (2)

feeela
feeela

Reputation: 29932

Yes, it's because you are using multibyte strings, in which a single character is represented by one to four bytes. If you select just one byte (as in $str[0]) you probably have only a half character selected. substr() instead is multibyte save and doesn't count the bytes, but the chars.

Upvotes: 0

Jon
Jon

Reputation: 437534

Indexing into the string with $str[0] pulls bytes out of it. It cannot be made aware of encodings, no matter that mbstring.func_overload has been set so. You will need to use substr even if it is not as convenient.

Indexing into a string is a grievous coding error unless that string represents a blob, and you just came upon the reason.

Upvotes: 1

Related Questions