Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mb_substr() not working correctly?

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...

like image 567
Alex Avatar asked Dec 19 '12 13:12

Alex


2 Answers

Try passing the encoding parameter to mb_substr, as such:

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

The encoding is never detected automatically.

like image 130
povilasp Avatar answered Nov 17 '22 11:11

povilasp


In practice I've found that, in some systems, multi-byte functions default to ISO-8859-1 for internal encoding. That effectively ruins their ability to handle multi-byte text.

Setting a good default will probably fix this and some other issues:

mb_internal_encoding('UTF-8');
like image 31
Álvaro González Avatar answered Nov 17 '22 11:11

Álvaro González