Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

migrated from webmasters.stackexchange.com Aug 12 at 12:52

This question came from our site for pro webmasters.

4 Answers

up vote 3 down vote accepted

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);
share|improve this answer
 
Is there an other way? What causes this anyway? Maybe a new PHP version, because this was not a problem lately? –  user2302838 Aug 13 at 19:54
 
Other way for what? and why? What certain problem you have at the moment? –  Your Common Sense Aug 13 at 19:56
 
I have already written a CMS system and it could not handle this issue... It would be a huge problem if I should to rewrite all the code. –  user2302838 Aug 13 at 20:32
 
Do you really have this silly syntax sugar more than once or twice in all the code? I just can't believe it. For what, on the Erath, purpose you are using it? –  Your Common Sense Aug 13 at 20:39

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

share|improve this answer

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

share|improve this answer
 
When I define $s[0]="á"; and echo $s[0]; it works perfectly, but no matter what I do, I don't get any á or strange characters. –  user2302838 Aug 12 at 16:58

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.