PHP mb_substr() not working correctly?

Alex picture Alex · Dec 19, 2012 · Viewed 21.6k times · Source

This code

print mb_substr('éxxx', 0, 1);

prints an empty space :(

It is supposed to print the first character, é. This seems to work however:

print mb_substr('éxxx', 0, 2);

But it's not right, because (0, 2) means 2 characters...

Answer

povilasp picture povilasp · Dec 19, 2012

Try passing the encoding parameter to mb_substr, as such:

print mb_substr('éxxx', 0, 1, 'utf-8');

The encoding is never detected automatically.