user2302838
user2302838

Reputation: 17

PHP string array UTF-8 encoding fails

Everything is set to UTF-8 (file encoding, MySQL [however I don't use it], Apache, meta, mbstring etc...) but check this out:

$s="áéőúöüóűí";
echo $s; //works perfectly

echo $s[0] // doesn't work. Prints out a single '?'.

I have tried almost everything. Any ideas? Thanks in advance!

Upvotes: 0

Views: 272

Answers (4)

psanjib
psanjib

Reputation: 1301

you have to make some changes in database go to the the table structure

you can find a column "Collation"

which column you want to change click edit on right side menu

the default Collation is - 'latin1_general_ci' change it to 'utf8_general_ci'enter image description here

Upvotes: -1

CCH
CCH

Reputation: 1536

And if you define $s[0]="á", does it work ? I believe that when encoded in UTF-8, those special chars are stored over two UTF-chars.

If you display in ANSI some UTF-8 text, it is rendered like this : áéoúöüóuí

You see that á becomes á So rendering the first char ($s[0]) would only display the "í", which is an incomplete character

Upvotes: 0

Your Common Sense
Your Common Sense

Reputation: 157828

It is absolutely correct behavior.

if you want to get a first letter from a multi-byte string, not first byte from binary string, you have to use mb_substr():

mb_internal_encoding("UTF-8");
echo mb_substr($s,0,1);

Upvotes: 3

Alma Do
Alma Do

Reputation: 37365

You should use mb_* functions for multibyte strings. mb_substr() in your case.

Upvotes: 2

Related Questions