0

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!

4 Answers 4

3

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);
4
  • Is there an other way? What causes this anyway? Maybe a new PHP version, because this was not a problem lately? Commented Aug 13, 2013 at 19:54
  • Other way for what? and why? What certain problem you have at the moment? Commented Aug 13, 2013 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. Commented Aug 13, 2013 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? Commented Aug 13, 2013 at 20:39
2

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

0

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

1
  • 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. Commented Aug 12, 2013 at 16:58
-1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.